Considering that everything in Ruby is an object and we can open irb and type things like 4.class and "Text".class to see from which class an object is, why do if.class and unless.class give no return value?
Considering that everything in Ruby is an object and we can open irb and
Share
That depends on your definition of “object” and every-“thing“. “Object” can mean “entity that can be manipulated by the program” (which I will call object from now on), or “value that is a member of the object system” (which I will call
Objectfrom now on).In Ruby, everything that can be manipulated by the program (i.e. every object) is also an
Object, i.e. an instance of a class. This is unlike Java, for example, where primitives can be manipulated by the program (i.e. are objects in that sense of the word), but aren’tObjects. In Ruby, this distinction doesn’t exist: every object is anObjectand everyObjectis also an object.However, there are things in the language, which cannot be manipulated by the program and which aren’t instances of a class, i.e. they are neither object s nor
Objects. These are, for example, methods, variables, syntax, parameter lists, argument lists, keywords.Note: you can use Ruby’s reflection API to give you an object that represents a method or a parameter list, but that object is only a proxy, it is not the real thing.
So, when we say “everything is an object“, what we really mean is that “every object is an
Object“, i.e. that everything which can be manipulated by the program is also a member of the object system, or in other words, there are no values outside of the object system (unlike primitives in Java). We do not mean that everything that exists in the language can also be manipulated at runtime by the program.Well, first off, even if
ifwere an object, those don’t do what you think they do: when you say something likefooin Ruby, it means either “dereference the local variablefoo” or “call the methodfoowithselfas the implicit receiver and no argument list”. So,would either give you the class of the object referenced by the local variable
ifor the class of the object returned by the methodif, but never the class ofifitself.But the
ifcontrol flow keyword isn’t an object, anyway (neither an object nor anObject) because keywords and control flow aren’t objects in Ruby.In the book The Ruby Programming Language by Matz and David Flanagan it says on page 2:
Note, it doesn’t say every-thing, only every value.
See also the question Is variable is object in ruby?