On Jul 26, 2011, at 3:39 PM, Glenn Little wrote:

> Just curious, is there a ruby best practice for effectively passing
> parameters by value to protect them from side effects?
> <snip>

> def recurse(x = nil)
>  y = x.dup
>  y = <some processing to reflect current node>
>  self.children.each do |c|
>    c.recurse(y)
>  end
> end


I do it as shown above. Make the method that would otherwise do the mangling, 
responsible for NOT mangling it. IMO, putting the dup as part of the parameters 
assumes unnecessary knowledge of how the method works. If you change the method 
some day, or want the mangling, you potentially have a bunch of code outside 
the method to change. Keep it encapsulated.

-- gw



-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby

Reply via email to