From: Simon Strandgaard Date: 2004-05-19T23:20:58+09:00 Subject: Re: Arguments and formal variables Angel Martin wrote: > In first place I sorprised with te behavior of formal variables inside a > metod or proc: > > > irb(main):020:0> a=proc { |i| p i.id;i=7;p i.id} > > => # > > irb(main):021:0> b=5 > > => 5 > > irb(main):022:0> b.id > > => 11 > > irb(main):023:0> a.call(b) > > 11 > > 15 > > => nil > > irb(main):024:0> b.id > > => 11 > > I google this and in the faq warn about it, calling it formal variables. > > Ok, if I want to change the value of a Array i can use Array#replace, > but is a way to change other types, Fixnum for example. > Is any way to do this?? You will need to box the value.. there are many way to do this, The most common quick and dirty solution is to use an Array, but you can also do: class Boxer def initialize(ref) @ref = ref end attr_reader :ref def set(ref) @ref = ref end end a=proc { |i| p i.ref.object_id # -> 11 i.set(7) p i.ref.object_id # -> 15 } b=Boxer.new(5) p b.ref.object_id # -> 11 a.call(b) p b.ref.object_id # -> 15 -- Simon Strandgaard