Can someone explain to me why Ruby treats integers and floating point
numbers differently?
irb(main):069:0> 12.equal? 12
=> true
irb(main):071:0> 12.0.equal? 12.0
=> false
Because they're different
With .equal you're checking to see whether 2 objects are in fact the
same object. 12, a Fixnum, is an immediate object, meaning all the
12s in your code are in fact the exact same object.
12.0, a floating point number, is constructed from scratch each time
you refer to it. So the 12.0 on the left is a separate construction
from the the 12.0 on the right.
irb(main):008:0> 12.object_id
=> 25
irb(main):009:0> a = 12
=> 12
irb(main):010:0> b = 12
=> 12
irb(main):011:0> a.object_id
=> 25
irb(main):012:0> b.object_id
=> 25
All of these 12's are the same object.
irb(main):013:0> 12.0.object_id
=> 11121408
irb(main):014:0> a = 12.0
=> 12.0
irb(main):015:0> b = 12.0
=> 12.0
irb(main):016:0> a.object_id
=> 11187424
irb(main):017:0> b.object_id
=> 10954912
All of these 12.0s are different objects
irb(main):018:0> 12 == 12
=> true
irb(main):019:0> 12.0 == 12.0
=> true
But all of the 12.0s have the same value.
The same distinction happens for symbols and strings:
irb(main):020:0> "abc".equal? "abc"
=> false
irb(main):021:0> :abc.equal? :abc
=> true