A
Adam Aa
Greetings,
I got a book on Ruby and I am trying to learn it piece by piece. I was
a Perl programmer for a while so the syntax of Ruby is not too hard for
me. What I don't understand is the following and I thank you in advance
for your help and input.
I have a variable named "num" that is set to "9." I'm using the
num.eql?() method to test whether or not num.eql?(9) and it gives me
"true" in IRB - no surprise there. The next one below also gives me
"true" since 18/2 is 9. I read in the book that if you want to force
the result of an experssion to be a float, make one or both operands a
floating number. So we have 18/2.0. But the result is "false." How come?
How come everytime I change either the number to float I get a false
even though the expression is correct? At first I thought I'm not
dividing numbers - rather maybe I'm dividing the value of the chars
rather than their math value. But that's not true as "18.to_i/2.to_i,"
just like "18/2," gives us "true."
I'm all ears to hear the logic behind such behavior
Thanks again
irb(main):058:0> num.eql?(9)
=> true
irb(main):059:0> num.eql?(18/2)
=> true
irb(main):060:0> num.eql?(18/2.0)
=> false
irb(main):061:0> num.eql?(18.0/2)
=> false
irb(main):062:0> num.eql?(18.0/2.0)
=> false
irb(main):063:0> num.eql?(18/2)
=> true
irb(main):064:0> num.eql?(18.0/2.to_f)
=> false
irb(main):065:0> num.eql?(18.to_f/2.to_f)
=> false
irb(main):066:0> num.eql?(18.to_f/2.to_i)
=> false
irb(main):067:0> num.eql?(18.to_i/2.to_i)
=> true
irb(main):068:0>
I got a book on Ruby and I am trying to learn it piece by piece. I was
a Perl programmer for a while so the syntax of Ruby is not too hard for
me. What I don't understand is the following and I thank you in advance
for your help and input.
I have a variable named "num" that is set to "9." I'm using the
num.eql?() method to test whether or not num.eql?(9) and it gives me
"true" in IRB - no surprise there. The next one below also gives me
"true" since 18/2 is 9. I read in the book that if you want to force
the result of an experssion to be a float, make one or both operands a
floating number. So we have 18/2.0. But the result is "false." How come?
How come everytime I change either the number to float I get a false
even though the expression is correct? At first I thought I'm not
dividing numbers - rather maybe I'm dividing the value of the chars
rather than their math value. But that's not true as "18.to_i/2.to_i,"
just like "18/2," gives us "true."
I'm all ears to hear the logic behind such behavior
Thanks again
irb(main):058:0> num.eql?(9)
=> true
irb(main):059:0> num.eql?(18/2)
=> true
irb(main):060:0> num.eql?(18/2.0)
=> false
irb(main):061:0> num.eql?(18.0/2)
=> false
irb(main):062:0> num.eql?(18.0/2.0)
=> false
irb(main):063:0> num.eql?(18/2)
=> true
irb(main):064:0> num.eql?(18.0/2.to_f)
=> false
irb(main):065:0> num.eql?(18.to_f/2.to_f)
=> false
irb(main):066:0> num.eql?(18.to_f/2.to_i)
=> false
irb(main):067:0> num.eql?(18.to_i/2.to_i)
=> true
irb(main):068:0>