Robert Klemme
7/27/2007 6:33:00 AM
2007/7/27, ashishwave <ashishwave@gmail.com>:
> if i pass arguments in a function then they get passed by value. how
> to get it passed by reference.
You can't. There are some solutions to this depending on situation:
1. inplace modification of arguments (Strings, Arrays, Hashes - all
sorts of containers)
2. multiple return values
3. work with instance variables
Generally in my experience it's not needed.
> For example: if i want to swrite my own swap function (not using
> multiple assignment syntax a,b=b,a) just for example example, how to
> implement that in ruby
irb(main):001:0> def swap(a,b) return b,a end
=> nil
irb(main):002:0> x,y=1,2
=> [1, 2]
irb(main):003:0> x,y = swap x,y
=> [2, 1]
irb(main):004:0> x
=> 2
irb(main):005:0> y
=> 1
But in reality you would just do
x,y = y,x
Kind regards
robert