Y
Y
Yauheni Dakuka2015-09-10 19:23:01
ruby
Yauheni Dakuka, 2015-09-10 19:23:01

Comparing object_id for float values?

There is a code:

a = 0.1
b = 0.1
a.object_id == b.object_id

Is it true or false because different irb's produce different values?
8c2d5b81b83d4a6e852970603f386a5d.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
thepry, 2015-09-10
@ydakuka

Different versions of ruby? Ruby 2 optimized float

# Ruby 1.9
4.2.object_id == 4.2.object_id # => false

# Ruby 2.0
warn "Optimization only on 64 bit systems" unless 42.size * 8 == 64
4.2.object_id == 4.2.object_id # => true (4.2 is immediate)
4.2e100.object_id == 4.2e100.object_id # => false (4.2e100 isn't)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question