How does Ruby ensure only one instance of each number?

Max

Suppose I have this simple code:

a = 1
b = 1

Does ruby ensure only one instance of numbers by tracking the numbers (so a and b would refer to the same object in memory) or just preventing users from creating numbers through the method new and duplicating/cloning them?

SteveTurczyn
a = 1
b = 1

Both a and b refer to the same object.

p a.object_id 
 => 3

p b.object_id
 => 3

So when you're comparing a and b Ruby compares the object ids (as it does with symbols) which makes comparison performant.

The object_id's of integers, interestingly enough, are always odd. Everything else has even object ids.

Note the comment from Holger Just below which goes into some detail regarding large integers.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

AWS ECS. How to ensure only one instance of a task is running?

How to ensure only one Play instance actually runs a method at one certain moment

How to ensure only one Azure Functions Event Grid Trigger instance is allowed to run at at time?

How can I ensure that only one instance of an Azure Synapse Pipeline modifies a lake database at the same time?

How do I list only one instance of each comment?

What is the best way to ensure only one instance of a Bash script is running?

How to ensure only one cron entry with Ansible?

How does "static" garantee that there is only one instance of a class in the property?

How does random number generation ensure reproducibility?

How does it ensure that one node is an ancestor and not a sibling?

How to ensure a specific class only can create an instance of another class?

Gremlin - how to ensure one vertex only have one inbound edge?

How to ensure exactly one service instance per docker swarm node

Does Class<SomeObject> have only one instance?

How to ensure each Controller can see the model and is only initialized once?

How to ensure that in pickerInput choices at least one item is selected in each group

Kubernetes : How to ensure one pod gets scheduled on each worker node?

How can I make only one instance of an application receive each AMQP topic message (consumer group behaviour)

How do I design the Storage class such that it accommodates any storage type and at the same time there is only one instance of each?

How does homebrew maintainers ensure the authenticity of binaries from each formula?

How to ensure only one request at the same time in Java

How to ensure that the variable was modified from only one side

How to ensure only one tab is coloured upon clicking?

How to ensure only one Azure Function BlobTrigger runs at a time?

How can I ensure that only one if a kind of Jenkins job is run?

MPI: How to ensure a subroutine is executed only on one processor on the default node?

How to ensure only one entry is True in a Django model?

For an array of instances, how does one, for each instance, invoke a certain method by this method's name?

Quick-and-dirty way to ensure only one instance of a shell script is running at a time