Prev: Processing (potentially) nil method without using nested if
Next: threads & mutex question (Rubyist book example)
From: Rein Henrichs on 5 Jun 2010 20:13 On 2010-06-05 17:02:17 -0700, Joel VanderWerf said: > Anyway, attr_accessor is already a private module method in ruby, but > the instance methods it creates are not, by default. I stand corrected. Interesting. -- Rein Henrichs http://puppetlabs.com http://reinh.com
From: Luis Lavena on 5 Jun 2010 20:33 On Jun 5, 6:25 pm, Joel VanderWerf <joelvanderw...(a)gmail.com> wrote: > Consider this error: > > $ cat t.rb > module M > private > def attr_accessor(*args); super; end > end > > class C > extend M > attr_accessor :x > end > > C.new.x = 1 > > $ ruby19 t.rb > t.rb:11:in `<main>': private method `x=' called for > #<C:0x0000000091c670> (NoMethodError) > > That looks very suspect, so I was about to file a bug report. But then I > noticed that there is a warning reported with the -v switch: > > $ ruby19 -v t.rb > ruby 1.9.2dev (2010-05-31) [x86_64-linux] > t.rb:3: warning: private attribute? > t.rb:11:in `<main>': private method `x=' called for > #<C:0x000000012e5670> (NoMethodError) > > Does anyone understand this warning? Is 'private' (without an argument) > deprecated? Maybe is because you're setting private for all the following methods, including the ones that attr_accessor defines moving forward after the "extending"? if you change the private to 'private :attr_accessor' there is no warning and things works. -- Luis Lavena
From: Joel VanderWerf on 5 Jun 2010 20:48 Luis Lavena wrote: > On Jun 5, 6:25 pm, Joel VanderWerf <joelvanderw...(a)gmail.com> wrote: >> Consider this error: >> >> $ cat t.rb >> module M >> private >> def attr_accessor(*args); super; end >> end >> >> class C >> extend M >> attr_accessor :x >> end >> >> C.new.x = 1 >> >> $ ruby19 t.rb >> t.rb:11:in `<main>': private method `x=' called for >> #<C:0x0000000091c670> (NoMethodError) >> >> That looks very suspect, so I was about to file a bug report. But then I >> noticed that there is a warning reported with the -v switch: >> >> $ ruby19 -v t.rb >> ruby 1.9.2dev (2010-05-31) [x86_64-linux] >> t.rb:3: warning: private attribute? >> t.rb:11:in `<main>': private method `x=' called for >> #<C:0x000000012e5670> (NoMethodError) >> >> Does anyone understand this warning? Is 'private' (without an argument) >> deprecated? > > Maybe is because you're setting private for all the following methods, > including the ones that attr_accessor defines moving forward after the > "extending"? > > if you change the private to 'private :attr_accessor' there is no > warning and things works. True. However: $ cat t.rb module M private def attr_accessor(*args); super; end public # <-- added this line end class C extend M attr_accessor :x end C.new.x = 1 $ ruby t.rb $ ruby19 t.rb t.rb:12:in `<main>': private method `x=' called for #<C:0x00000001e40858> (NoMethodError)
From: Joel VanderWerf on 5 Jun 2010 21:06
Luis Lavena wrote: > Maybe is because you're setting private for all the following methods, > including the ones that attr_accessor defines moving forward after the > "extending"? That's probably true in some sense (some state is getting set and affecting the methods defined by attr_accessor), but it's not setting other methods private: $ cat t.rb module M private def attr_accessor(*args); super; end end class C extend M def m; puts "m"; end attr_accessor :x end C.new.m C.new.x = 1 $ ruby19 t.rb m t.rb:13:in `<main>': private method `x=' called for #<C:0x00000002442148> (NoMethodError) |