From: "David A. Black" Date: 2008-09-09T07:32:34+09:00 Subject: Re: "Pointer" to an object? (i.e. reflect changes to origina Hi -- On Tue, 9 Sep 2008, Doug Glidden wrote: > Calamitas wrote: > [snip] >> Or closer to the pointer solution you want: >> >> p.primary_email = lambda { p.personal_email } >> p.personal_email = 'me@gmail.com' >> puts p.primary_email.call >> >> The lambda expression corresponds to the address operator, the .call >> corresponds to a pointer dereference. >> >> Peter > > Peter, > > Thanks! I had just (literally within the last ten minutes) thought of > the possibility of using an enclosure of some sort to do the trick. I > was playing around with using a proc, you used a lambda, I think either > one will probably work with some tweaking. This is the "perfect" > solution, I think. > > I'm a little curious how this will react if p.primary_email.call is > executed when p is not in scope, e.g. if something like the following > occurs, so I'm going to play around: > > def do_something(person) > email = person.primary_email.call > # do more stuff... > end > > p = Person.new > p.personal_email = 'me@home.com' > p.primary_email = lambda {p.personal_email} > do_something p > > Will the lambda be able to handle that situation? Like I said, I'm > going to play around with it. Yes; the lambda carries its local scope with it, so wherever it's called, it will have access to the p that it started with. 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 * * Co-taught with Patrick Ewing! See http://www.rubypal.com for details and updates!