i have following code:
def mymethod(a)
  a.replace("a")
end
mystring = "b"
mymethod(mystring) 
p mystring # => "a"
but i want to perform same with Integer
is that possible?
Short answer: no.
Long answer: no, it's not possible. Integer is a type primitive enough to not have state (and state modifying operations). Every operation on integer generates a new integer.
Probably, if you drop down to C level, you could be able to modify underlying value in-place. But I'm not sure. Anyway, this seems like an overkill and a wrong thing to do.
-1 for "wrong thing to do". This can be a perfectly reasonable issue -- I need to do this right now: multiple objects need a shared counter.
It seems to me that the best way is to create a wrapper class and have the integer as an instance variable:
class Counter
  def initialize(i = 0)
    @i = i
  end
  def get
    @i
  end
  def set(i)
    @i = i
  end
  def inc(delta = 1)
    @i += delta
  end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With