> And C, Python, and Ruby probably won't let you do that...
From my understanding, in Ruby all argument passing is by value.
However, the value passed may not be what you expect... The value that
is passed is that of the reference, not the value that the reference
refers to. So, for example:
def foo(arg)
arg = 3
end
a = 'Mike'
foo(a)
This will not change the value of a in the top level. Why? Because foo
reassigns the argument reference arg to 3, a different object than
'Mike.' However:
def bar(arg)
arg[0] = 'b'
end
a = 'Mike'
bar(a)
This will change a from 'Mike' to 'bike.' Why? Because instead of
reassigning arg to a new object, the object itself ('Mike') has been
modified through the method call []. So, if the method simply reassigns
the reference, there will be no change in the original (it still refers
to the same object). But, if the method modifies that object, the
original reference will reflect that modification, since it is still
referencing the now modified object.
It is customary in Ruby to append an exclamation mark (!) to indicate
methods that actually modify an object in place. So, bar! would have
been a more appropriate name for the second method.
Hope that helps.
David B. Williams
http://www.cybersprocket.com
--
Posted via http://www.ruby-forum.com/.