Hi -- On Mon, 20 Jul 2009, Frederick Cheung wrote:
> > > > On Jul 20, 11:20 pm, Learn By Doing <[email protected]> wrote: >> Hi David, >> >> The reason I need to redefine the assignment and make it private is >> that because whenever x is assigned a new value, some other processes >> must take place: > > Why not override x= then rather than inventing a new set_x method ? > >> >> def set_x(val) >> self.x = val >> # some other processes take place here >> end >> >> private >> >> def x=(val) >> super >> end >> >> I don't understand when you say that there is no built-in method for >> "x=". How else can one assign value to an attribute? > > The setter methods get generated on the fly for you (the default ones > just end up calling write_attribute) as David explained - you can't > mark them as private or override and use super to call the original > implementation because the method doesn't exist yet. You can do this and get, by coincidence, something like the desired effect: def x=(val) super # do other stuff end because the call to super will trigger AR::B#method_missing, but the more I look at it and play around with it, the more I think it's way too fragile and too closely couple to the method_missing implementation to use. David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Now available: The Well-Grounded Rubyist (http://manning.com/black2) Training! Intro to Ruby, with Black & Kastner, September 14-17 (More info: http://rubyurl.com/vmzN) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---

