From: dblack@... Date: 2007-01-27T20:46:47+09:00 Subject: Re: pass by reference? ---2049402039-1339491571-1169898401=:16717 Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-1339491571-1169898401=:16717" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. ---2049402039-1339491571-1169898401=:16717 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Sat, 27 Jan 2007, Wolfgang N=E1dasi-Donner wrote: >>>>>> Code >>>>> > > def modifying(a_param) > a_param =3D "hi" > end > > def modifying2(a_param=3Da_value) > a_param =3D "hi" > end > > def modifying3(a_param) > a_param[0..-1]=3D "hi" > end > > a_value =3D "low" > modifying a_value > case a_value > when "hi" > puts "Passed by reference" > when "low" > puts "Passed by value" > else > puts "Passed away" > end > > a_value =3D "low" > modifying2 a_value > case a_value > when "hi" > puts "Passed by reference" > when "low" > puts "Passed by value" > else > puts "Passed away" > end > > a_value =3D "low" > modifying3 a_value > case a_value > when "hi" > puts "Passed by reference" > when "low" > puts "Passed by value" > else > puts "Passed away" > end > >>>>>> Output >>>>> > > Passed by value > Passed by value But you've broken the bindings. It's a whole new ballgame, as we say here :-) > Passed by reference I would say: a reference, passed by value. >>>>>> EoE >>>>> > > I would name it somehow "call by assignment of value to local variable",= =20 > because the value of the actual parameter is assigned to the local variab= le=20 > used as a formal parameter. > > except for "Fixnum" etc. this is a pointer to an object. I one changes th= e=20 > object itself, it will work like "by reference", otherwise only the loca= l=20 > variable is affected, without having any effect to the outside world. I consider the variable assignment semantics to be a separate matter. For example: def x(a) a =3D "hello" end Here, Ruby's rule is that the second a is a completely new binding, and destroys the original binding. I don't think that has anything to do with whether the first a is a reference or a value. In other words, even if Ruby had pass by reference, I assume that a =3D "hello" would still destroy the binding and create a new local variable. (Of course, it's purely speculative, since it would be a completely different language.) David --=20 Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com) ---2049402039-1339491571-1169898401=:16717-- ---2049402039-1339491571-1169898401=:16717--