# How to access a classes private method

**URL:** <https://www.ruby-forum.com/t/how-to-access-a-classes-private-method/95281>\
**Category:** Ruby\
**Created:** [May 26, 2006, 2:30pm UTC](https://www.ruby-forum.com/t/how-to-access-a-classes-private-method/95281 "2006-05-26T14:30:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [May 26, 2006, 2:30pm UTC](https://www.ruby-forum.com/t/how-to-access-a-classes-private-method/95281/1 "2006-05-26T14:30:26Z")

</div>

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 resulting in a method not found error. I know I can’t access a  
protected method this way, but I don’t know how to open up the class and  
access this method. Thanks in advance for any help.

Charlie B.  
[http://www.recentrambles.com](http://www.recentrambles.com)

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [May 26, 2006, 2:33pm UTC](https://www.ruby-forum.com/t/how-to-access-a-classes-private-method/95281/2 "2006-05-26T14:33:34Z")

</div>

Sorry, just realized that the subject said private method, I meant to  
say protected.

On Fri, 2006-05-26 at 21:29 +0900, Charlie B. wrote:

> end  
> access this method. Thanks in advance for any help.
> 
> Charlie B.  
> [http://www.recentrambles.com](http://www.recentrambles.com)

Charlie B.  
Programmer  
Castle Branch Inc.

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [May 26, 2006, 2:58pm UTC](https://www.ruby-forum.com/t/how-to-access-a-classes-private-method/95281/3 "2006-05-26T14:58:59Z")

</div>

Charlie B. wrote:

> Sorry, just realized that the subject said private method, I meant to  
> say protected.
> 
> On Fri, 2006-05-26 at 21:29 +0900, Charlie B. wrote:
> 
> > end  
> > access this method. Thanks in advance for any help.
> > 
> > Charlie B.  
> > [http://www.recentrambles.com](http://www.recentrambles.com)
> 
> Charlie B.  
> Programmer  
> Castle Branch Inc.

If you need to access it outside of an instance, why make it protected?  
How are you using this?

Anyway, onto the solution:

def calling\_protected\_method  
result = Foo::Bar.new.method(:bars\_method).call  
end

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [May 26, 2006, 3:17pm UTC](https://www.ruby-forum.com/t/how-to-access-a-classes-private-method/95281/4 "2006-05-26T15:17:02Z")

</div>

…

> ```
> def bars_method(a=2)
> a
> end
> 
> ```

…

> Here’s what the relevant parts of my script look like
> 
> def calling\_protected\_method  
> result = Foo::Bar.bars\_method  
> end

You do realize that the whole point of making methods protected is NOT  
to be able to call them, right?  
-tim

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [May 26, 2006, 3:20pm UTC](https://www.ruby-forum.com/t/how-to-access-a-classes-private-method/95281/5 "2006-05-26T15:20:36Z")

</div>

Yes, I’m assuming the reason this method is marked as protected in the  
rails framework is so that it can’t be called arbitrarily from the web.  
I don’t think I’ll be causing any trouble by calling it from a plugin.

On Fri, 2006-05-26 at 22:15 +0900, Tim B. wrote:

> You do realize that the whole point of making methods protected is NOT  
> to be able to call them, right?  
> -tim

Charlie B.  
[http://www.recentrambles.com](http://www.recentrambles.com)

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [May 26, 2006, 3:26pm UTC](https://www.ruby-forum.com/t/how-to-access-a-classes-private-method/95281/6 "2006-05-26T15:26:57Z")

</div>

On May 26, 2006, at 8:29 AM, Charlie B. wrote:

> ```
> 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 #\<Foo::Bar:  
0x1c8e50\>

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 W.
