From: gwtmp01@... Date: 2006-05-26T22:25:58+09:00 Subject: Re: How to access a classes private method On May 26, 2006, at 8:29 AM, Charlie Bowman wrote: > I'm trying to call a classes protected method but I can't seem to > get it > to work. Here's an example of the method I'm trying to call > > moule Foo > Class Bar > protected > def bars_method(a=2) > a > end > end > end > > Here's what the relevant parts of my script look like > > def calling_protected_method > result = Foo::Bar.bars_method > end This is wrong for two reasons: 1) you are calling bars_method on the class object 'Foo::Bar' and not on an *instance* of Foo::Bar 2) the standard calling syntax will trip the protected method check bar = Foo::Bar.new # get an instance of Foo::Bar bar.bars_method # method found now but is protected NoMethodError: protected method `bars_method' called for # In order to bypass the protected method check you need to use send, which ignores visibility attributes (private/protected). bar.send(:bars_method) # 2 bar.send(:bars_method, "arg1") # "arg1" Gary Wright