Page 25 of 25

Re: middleclass & extras: middleclass 3.0 is out!

Posted: Fri Aug 01, 2014 3:16 pm
by kikito
No, I meant that, in Ruby, classes are instances of a class named Class.

I will try to give an example. The code in this post will be all ruby.

Imagine that you create a class named Person.

In ruby, classes have an attribute called name which returns their name (It's a bit magical). So if you do Person.name, you get the string "Person".

Imagine that you create an instance of Person, doing peter = Person.new(). Nothing extraordinary there.

Since it is an instance, peter has a built-in attribute named peter.class. It contains a reference to Person.

But, if you do Person.class, you also get a reference to something - a different class. That's the Class class. You can check it by doing Person.class.name (it returns the string "Class").

In ruby, everything is an object. This means that everything is an instance of some class. Even classes. Even the Class class is an instance of something (it's an instance of itself - nifty stuff).

This is very useful because it means that you can treat classes as objects. You can put them on an array, and call methods on them. You can ask a class "give me your list of methods", and then look for a particular method, and call it. Etc.

What I was trying to say is that I had to let go of that complexity when I built middleclass. Everything on middleclass isn't an object. In particular, classes are not instances of anything, and there is no Class class.

I hope this clarifies things.

Re: middleclass & extras: middleclass 3.0 is out!

Posted: Fri Aug 01, 2014 3:48 pm
by Roland_Yonaba
kikito wrote:I hope this clarifies things.
Hey, I learnt something indeed, here. Awesome.

Re: middleclass & extras: middleclass 3.0 is out!

Posted: Fri Aug 01, 2014 4:18 pm
by murks
Thanks, that explains it. I didn't realise that ruby is _that_ weird, but it seems to consequently realize 'everything is an object'.

Re: middleclass & extras: middleclass 3.0 is out!

Posted: Sat Aug 02, 2014 9:13 am
by bartbes
I must have had ruby confused with another language, then, my bad.