From: "David A. Black" Date: 2005-01-19T10:38:47+09:00 Subject: Re: value by reference Hi -- On Wed, 19 Jan 2005, Mohammad Khan wrote: > Before telling my issue, let me show my script: > > > def do_something(a, b, c) > a = a + 1 > b = b + 2 > c = c + 3 > end > a, b, c = 5, 6, 7 > puts "before do_something:" > puts "a : #{a}" > puts "b : #{b}" > puts "c : #{c}" > do_something(a, b, c) > puts "after do_something:" > puts "a : #{a}" > puts "b : #{b}" > puts "c : #{c}" > > > > My issue: > I would like to see the value of a, b, c after calling #do_something as: > a = 6, b = 8, c = 10 > > how can I do it without making a, b, c an instance variable? > In other words, how can I send value by reference? Fixnums are immediate values, and are immutable, even when they're wrapped in variables. So what you're trying to do is equivalent to: do_something(5,6,7) puts 5 * 5 # 36 -- i.e., 5 has become 6 It won't work (luckily :-) because Ruby doesn't let you perform that kind of modification on Fixnums. With mutable objects you can do what you're describing -- for example, adding to a string that's in a local variable: irb(main):001:0> def x(y); y << "def"; end => nil irb(main):002:0> s = "abc" => "abc" irb(main):003:0> x(s) => "abcdef" irb(main):004:0> s => "abcdef" David -- David A. Black dblack@wobblini.net