0

Well, I can iterate over 3 to 101 and select only the odds using:

(1..50).to_a.map(&:object_id)

or

(1..50).map(&:object_id)

or even like this:

(1..50).to_a.each do |c| p c.object_id end

And the ways to do the same goes on and on...

The question comes to my mind if "A" can have any object id of Integer class, and seems quite random, then why does a number have fixed ones?

  • 2
    The short answer is: Because my `1` is the same as your `1`. There is only one `1`. Every time you reference the number `1`, it is always **the same object**. Strings (in ruby), on the other hand, and mutable; if you create two different (identical) strings, in different places, then they are **different objects**. You can change one, without changing the other. Therefore, they must have different `object_id`s. – Tom Lord Sep 07 '18 at 10:17

1 Answers1

1

The object ID is calculated from the objects value plus some additional information. From that calculation you can derive the values you are seeing in your examples.

for more information in details read these SO post:

Gagan Gami
  • 10,121
  • 1
  • 29
  • 55