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?
相关问题
- how to define constructor for Python's new Nam
- Keeping track of variable instances
- Object.create() bug?
- std::vector of objects / pointers / smart pointers
- How to specify memcache server to Rack::Session::M
相关文章
- 接口B继承接口A,但是又不添加新的方法。这样有什么意义吗?
- Ruby using wrong version of openssl
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- Segmentation fault with ruby 2.0.0p247 leading to
- How to detect if an element exists in Watir
- uninitialized constant Mysql2::Client::SECURE_CONN
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
Object
from 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 anObject
and everyObject
is 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
Object
s. 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
if
were an object, those don't do what you think they do: when you say something likefoo
in Ruby, it means either "dereference the local variablefoo
" or "call the methodfoo
withself
as the implicit receiver and no argument list". So,would either give you the class of the object referenced by the local variable
if
or the class of the object returned by the methodif
, but never the class ofif
itself.But the
if
control 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?
If you observe the question mark and the indentation, when IRB detects something that needs continuation, it indents.
As you can see, IRB does not immediately answer, even if there is an obvious mistake such as sending a message to a reserved word. The ? is the sign that it is waiting for the rest of the statement.
Because they are language keywords and not objects. In general, everything you can assign to a variable is an object.
To be technically correct: keywords such as
unless
might have a class that is used by compiler/interpreter (depends on an actual implementation), but you, as a language user, don't have any even remotely practical use for it and so it's not exposed to you.