From: Marnen Laibow-Koser Date: 2010-01-18T11:33:17+09:00 Subject: Re: basic question: passing a modifiable argument to a routi lalawawa wrote: > Arguments to subroutines They're called functions or methods in Ruby. > seem to be passed by value in ruby Nope. They're passed by reference -- or rather, they're object references passed by value, like in Java. > > $ irb > irb(main):001:0> def woof(a) > irb(main):002:1> a += 2 > irb(main):003:1> end > => nil > irb(main):004:0> b = 5 > => 5 > irb(main):005:0> woof(b) > => 7 > irb(main):006:0> b > => 5 > irb(main):007:0> > Yes, because += makes a reference to a different object. > so how do I pass a variable to a routine if I want the routine to > modify the variable. Don't. Instead, assign the return value to the variable, or use a bang! method on an object. > In C I would say: > > void woof(int *a) { > *a += 2; > } > int b = 5; > woof(&b); > printf("%d\n", b); > > would output 7. b = woof(b) Best, --  Marnen Laibow-Koser http://www.marnen.org marnen@marnen.org -- Posted via http://www.ruby-forum.com/.