How the equality check is being done in Ruby?

Hi ,

I ran the below code to do some test on <=>,==,===,eql?,equal? I took 1
and 1.o to perform my test.

Part-I : output is saying 1 and 1.0 are equal - as per mathematical
knowledge it is right.

irb(main):009:0> 1<=>1.01
=> -1
irb(main):010:0> 1<=>1.0
=> 0
irb(main):011:0> 1==1.0
=> true
irb(main):012:0> 1===1.0
=> true

Part- II

Here why the opposite output comes here, how it comes? what computation
Ruby did on these?

irb(main):015:0> 1.eql? 1.0
=> false
irb(main):016:0> 1.equal? 1.0
=> false
irb(main):017:0>

···

--
Posted via http://www.ruby-forum.com/.

Someone's done a handy description of some common comparison operators
here:

···

--
Posted via http://www.ruby-forum.com/.

Hi Arup,

"eql?" returns true if num and numeric are the same type and have equal values.
1 is Integer, 1.0 is Float

1 == 1.0 #=> true
1.eql?(1.0) #=> false
(1.0).eql?(1.0) #=> true

please refer to the documentation before asking.

regards
attila

···

On Sat, Jan 12, 2013 at 10:13 PM, Arup Rakshit <lists@ruby-forum.com> wrote:

Hi ,

I ran the below code to do some test on <=>,==,===,eql?,equal? I took 1
and 1.o to perform my test.

Part-I : output is saying 1 and 1.0 are equal - as per mathematical
knowledge it is right.

irb(main):009:0> 1<=>1.01
=> -1
irb(main):010:0> 1<=>1.0
=> 0
irb(main):011:0> 1==1.0
=> true
irb(main):012:0> 1===1.0
=> true

Part- II

Here why the opposite output comes here, how it comes? what computation
Ruby did on these?

irb(main):015:0> 1.eql? 1.0
=> false
irb(main):016:0> 1.equal? 1.0
=> false
irb(main):017:0>

--
Posted via http://www.ruby-forum.com/\.

Joel Pearson wrote in post #1092080:

Someone's done a handy description of some common comparison operators
here:

Thanks for the link,most of the part going above the head! :frowning: trying to
map it with my one,but my bad.I was failed.

···

--
Posted via http://www.ruby-forum.com/\.

I think you need to start here;

http://pine.fm/LearnToProgram/

···

On Jan 12, 2013, at 3:29 PM, Arup Rakshit wrote:

Joel Pearson wrote in post #1092080:

Someone's done a handy description of some common comparison operators
here:

ruby - What's the difference between equal?, eql?, ===, and ==? - Stack Overflow

Thanks for the link,most of the part going above the head! :frowning: trying to
map it with my one,but my bad.I was failed.