Reputation: 756
In the tutorial Rubymonk says :
"In Ruby, just like in real life, our world is filled with objects. Everything is an object - integers, characters, text, arrays - everything."
But && and || are operators, operators are objects too ? how check ?
this is not going against the philosophy of ruby "All is a object" ?
Upvotes: 2
Views: 504
Reputation: 31471
In Ruby, all variable types refer to objects (like Python). But operators are not objects. You can check by trying to call the to_s() method on them.
Upvotes: 0
Reputation: 20857
It depends on how you define "everything". I personally would never claim that each individual concept in ruby is an object. I suppose there isn't really a good definition to describe how ruby works. I think you just have to accept the way ruby does it, which will become plain as you use it.
If you want to see if something is an object, try assigning it to a variable, or calling a method on it. You'll get an error if you try to do that with an operator. A notable exception is methods, which aren't objects but return objects.
Note that the concept of an object is somewhat abstract. Two variables can point to the same object. Every object is represented by an object_id
. You might think of an object_id like a location in memory. Or, you can think of an object as a house, and multiple address books can contain the house's address.
# point 2 variables to the same object
>> s = t = 's'
=> "s"
>> s.object_id
=> 70269794388360
>> t.object_id
=> 70269794388360
# get the object by id (for demonstration only; I don't recommend using this in application code)
>> ObjectSpace._id2ref(70269794388360)
=> "s"
# modifying the object. both variables "see" the change
>> s << '_'
=> "s_"
>> t
=> "s_"
Continuing the house analogy, appending to a string via <<
is like bringing a new chair into the house. Everyone who follows their address book to that house (using the address) will see that the house (object) has the new chair.
The ruby language designers did not see a reason to allow treating operators like &&
as objects. Can you see any reason to allow it?
Upvotes: 1
Reputation: 9841
Operators are not objects, most operators in Ruby are methods.
You can check it by running irb
and looking at public_methods
of some object:
$ irb
irb(main):001:0> "a"=="b"
=> false
irb(main):002:0> "a".public_methods
=> [:<=>, :==,.........
And what about operators from the question, &&
and ||
is hardcoded into Ruby language, but ==
is mixed in from Comparable
module.
Upvotes: 1
Reputation: 5237
Most Operator in ruby are actually method calls.
2.+(3) # 5
Here's a good explanation : http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-operators-and-their-methods/
Upvotes: 0
Reputation: 84343
&&
and ||
Tokens are OperatorsThe &&
and ||
tokens are not objects, they are operators defined by Ruby's parser. You can dig through the source code to find the actual definitions and implementation details, if you're interested. To get you started, here's a regular expression match to get you started in reviewing the source:
$ fgrep -n '%token tANDOP tOROP' ruby-1.9.3-p194/ext/ripper/ripper.y
719:%token tANDOP tOROP /* && and || */
Upvotes: 4