Question about the threequal operator .===

J

John

Hi

I'm a newbie on ruby and i have some questions :
*What is special to the threequals operator when it comes to when
clause ?
object === other_object

*What is the "." in front of the operator ?
object .=== other_object
Thanks
John
 
R

Robert Klemme

Hi

I'm a newbie on ruby and i have some questions :
*What is special to the threequals operator when it comes to when
clause ?
object === other_object

It's simply the fact that it's invoked. "when" doesn't invoke ==, eql?
or equal? but ===.
*What is the "." in front of the operator ?
object .=== other_object

This is just the normal method invocation syntax. Note that an operator
in Ruby is nothing else than an instance method with a special
invocation syntax.
10:25:00 [~]: irbs
o = Object.new
=> # said:
def o.===(x) p x end => nil
o === 1
1
=> nil1
=> nil1
=> nil1
=> nil

These are all equivalent pieces of code.

Kind regards

robert
 
A

Austin Ziegler

It's simply the fact that it's invoked. "when" doesn't invoke ==, eql?
or equal? but ===.

And it does it differently than might be expected.

case object
when other_object
end

is the same as other_object === object, not the other way around.

-austin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,209
Messages
2,571,089
Members
47,687
Latest member
IngridXxj

Latest Threads

Top