From: Brad Date: 2003-12-14T07:26:58+09:00 Subject: Re: Simple issue giving problems Jim Weirich wrote: > Brad wrote: > > > What I was trying to say was... > > > > I was wondering if there were some way to allow manipulations of > > function/methods parameters such that the calling processes variable > > values would be modified. > > > > As I understand it now this is not possible, though being new to Ruby I > > thought it couldn't hurt to ask. > > No, it doesn't hurt to ask. > > There is a way of modifying the variable bindings in the calling method, > but it is not widely used. You need to create a "reference" to the > variable and pass that into the swap function. I use a "helper" > function "ref" to create the reference ... > > Here's an example ... > > require 'reference' > > def swap(a,b) > a.value, b.value = b.value, a.value > end > > x = 10 > y = 11 > > puts "BEFORE: x = #{x}, y = #{y}" > swap(ref{:x}, ref{:y}) > puts "AFTER: x = #{x}, y = #{y}" > > Note that those are curly braces, not parenthesis after "ref". This > will print ... > > BEFORE: x = 10, y = 11 > AFTER: x = 11, y = 10 > > Here's the reference.rb file that you will need ... > > class Reference > def initialize(&block) > sym = block.call > fail "Block must yield symbol" unless Symbol === sym > @getter = eval "proc { #{sym} }", block > @setter = eval "proc { |v| #{sym} = v }", block > end > def value > @getter.call > end > def value=(v) > @setter.call(v) > end > end > > def ref(&block) > Reference.new(&block) > end > > Jim: Thanks for the reply, it's appreciated. I think this may be what I'm looking for. I'll give it a try and get back to you with the results. Thanks, Brad