From: "David A. Black" Date: 2008-08-28T20:03:29+09:00 Subject: Re: how to avoid passing by reference and how to copy objects Hi -- On Thu, 28 Aug 2008, Adam Akhtar wrote: > thanks lex for that. > Im going to search for "ruby program design principles" or patterns > /best practices with regards to functions. When i learnt C (10 years ago > so its rusty) I was told to use pass by reference only when necessary. I > think the reason being bug tracking etc maybe the reasoning was similiar > ot that of not to over/misuse global variables. > > So switching to ruby where pass by reference is the only choice seems to > be making my programming a little less smooth. Im just used to knowing > that the original value wont be changed. Sure I could dup or clone > everything but that seems to go against what ruby intends. See my earlier post, and Brian's last one. It's not pass by reference; it's pass by value, where the values are references. That's actually good news. For one thing, you don't have to do (indeed can't do) pointer arithmetic. Every reference is exactly one step away from its object. There are no references to references (which is why you can't do pass-by-reference in Ruby). C is actually a poor point of reference (ha ha) for understanding Ruby variables and references, because the two are so different. I'd encourage you not to worry about how Ruby semantics align with C semantics, because they pretty much don't. In C you have this: ***p -> **p -> *p -> p but in Ruby you always have this: b | v a-> obj <- c ^ | d David -- Rails training from David A. Black and Ruby Power and Light: Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL Advancing with Rails January 19-22 Fort Lauderdale, FL See http://www.rubypal.com for details and updates!