3

How can I increment an Integer variable by X without creating a new object instance?

+= does not work because:

ree-1.8.7-2010.02 > x = 1
1
ree-1.8.7-2010.02 > x.object_id
3
ree-1.8.7-2010.02 > x += 1
2
ree-1.8.7-2010.02 > x.object_id
5
1
  • Are you asking out of curiosity, or is there a use case for this (such as concurrent programming)? Commented Nov 23, 2010 at 22:36

5 Answers 5

2

You can't. Not in Ruby, and not in any other programming language I am aware of.

The object which represents the mathematical number 1 will always have the value 1. Mutating the object which represents the mathematical number 1 to suddenly have the value 2 would quite simply be insane, because now all of a sudden 1 + 1 == 4.

Sign up to request clarification or add additional context in comments.

2 Comments

And what's wrong with 1 + 1 == 4? They do it in government and corporations all the time! :-)
You can mutate this in Python and I would guess also in Ruby - also this answer completely fails to address why this is the case. Consider the difference a=12; b=12; b.object_id; a.object_id; vs (x = 9**57; y= 9**57; y.object_id; x.object_id)
2

Extend your example for a moment. Try this:

x = 2
y = 1 + 1

x.object_id
y.object_id

Every unique number will have its own identity. Ruby's object orientedness goes a bit deeper than you will find with C++ and Java (both of those have the concept of primitives and classes).

What's important is that when you query x the second time for its value the value will be what you expect. Object identifiers don't really matter unless you are the garbage collector.

2 Comments

In YARV ruby, x and y both have object_ids of 5. Are they different to each other in your implementation?
That was the point. The value 2 will have the same instance id, regardless of how you got there. In the end, object ids mean nothing--it's the variable you care about.
0

It gets worse with Bignums

begin
  a = 1234567890
  puts a.object_id
  b = 1234567890
  puts b.object_id
end

gave me

10605136
10604960

Comments

0

Execution time is really terrible even if just you organize a simple loop. Primitives shouldn't be ditched from Ruby.

(1..16000).each do
  (1..16000).each do
  end
end

This itself takes 30-40 seconds to complete (Lenovo T400, Virtualboxed Ubuntu), and you haven't even done something sophisticated.

Comments

0

You can use a helper class:

class Variable
  def initialize value = nil
    @value = value
  end

  attr_accessor :value

  def method_missing *args, &blk
    @value.send(*args, &blk)
  end

  def to_s
    @value.to_s
  end

  # here's the increment/decrement part
  def inc x = 1
    @value += x
  end

  def dec x = 1
    @value -= x
  end
end

x = Variable.new 1
puts x               #=> 1
puts x.object_id     #=> 22456116 (or whatever)

x.inc
puts x               #=> 2
puts x.object_id     #=> 22456116

x.inc 3
puts x               #=> 5
puts x.object_id     #=> 22456116

More uses of "class Variable" here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.