Hi -- On Sun, 19 Jul 2009, Learn By Doing wrote:
> > On Jul 19, 6:39 pm, "David A. Black" <dbl...@rubypal.com> wrote: >> Hi -- >> >> >> >> On Sun, 19 Jul 2009, Ease Bus wrote: >>> Hi, >> >>> I would like to rename the setter of "attribute_A" with the following: >> >>> def set_attribute_A(val) >>> self.attribute_A = val >>> end >> >>> private >> >>> def attribute_A=(val) >>> super >>> end >> >>> But then I get "NoMethodError: Attempt to call private method" I thought >>> that a private method is >>> accessible from within the same instance. >> >> There's not quite enough information in the code you've got here to >> see what's going on. Do you have a superclass with a definition for >> the attribute_A= method? (I'm trying to figure out why you're calling >> super.) >> > > Hi David, > > Thanks for your response. attribute_A is just an attribute in a model > that is a subclass of ActiveRecord::Base. attribute_A is of type > integer. > > The reason I used "super" is because I just wanted to assign the value > of "val" to attribute_A in the private setter method "attribute_A=" I > can't say self.attribute_A = val because that is calling the private > method I am defining. I'm trying to duplicate the error message you're getting, but I can't quite get my example code to do that. On the other hand, I believe there are other issues.... If you define, say, x= to call super, the problem is that you'll hit method_missing (because there's no built-in x= method). And method_missing will obligingly define x= for you, and not make it private. That's just a result of how AR implements method_missing; it does some on-the-spot definition of attribute methods. I suspect you're going to have trouble with this whole thing, because you're going up against AR's internals. I may well be overlooking something, but that's my (late at night :-) impression. I can't help wondering whether you absolutely need to do this. The whole "set_x" naming scheme is very unidiomatic in Ruby, and the convention of the = methods acting like assignments, in their return semantics, is generally not considered a problem. Is there something in that original goal that you could reassess? 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 rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---