Hi --

On Sat, 25 Aug 2007, John Dearden wrote:

> Logan Capaldo wrote:
>> or you can return more than one value
>>
>> def munge(a, b)
>>    return a + 1, b - 2
>> end
>>
>> q = 4
>> r = 9
>>
>> q, r = munge(q, r)
>
> Ah, well, now we're on to something!  It looks a bit clunky to my
> untrained eye, but it will definitely do the job, and honors the Ruby
> 'local variables are local' way.

Don't worry; Ruby won't let you dishonor that :-)

Keep in mind that most of the time, you'll probably be assigning the
results to other variables. Using my earlier example:

   def change_me(a)
     a + 2
   end

you'd then probably do something like:

   n = 2
   m = change_me(n)  # as opposed to reassigning to n

which looks a little less clunky and, in a real program, would
probably make lots of sense. Both I and Logan (I surmise) provided you
with examples that did the reassigning to the same variable, just to
match what you were doing as closely as possible, but it's a general
technique.

Of course, you'll also see a lot of this:

   n = m.change_me

depending on exactly what's going on.


David

-- 
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
   RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
     & consulting:  Ruby Power and Light, LLC (http://www.rubypal.com)