Martin DeMello
5/23/2006 6:35:00 AM
Martin <MartinKess@gmail.com> wrote:
> How would I make something like this do what I want it to?
> ----
> def foo!(a)
> a = "bar"
> return "baz"
> end
>
> change_me = "hello"
> some_var = foo!(change_me)
>
> puts change_me #=> "bar"
> puts some_var #=> "baz"
The short answer is that you can't - variables themselves are not
objects, so you can't pass a variable to a function and have it rebind
it to another object. What you *can* do is change the contents of the
object that you pass in to the function. Your class needs to define a
'replace' method, so that you can say
def foo!(a)
a.replace('bar')
return 'baz'
end
Hash, Array and String have replace methods defined for them, so your
client code should work as written.
martin