# Self and Protected Methods question/confusion

**URL:** <https://www.ruby-forum.com/t/self-and-protected-methods-question-confusion/177942>\
**Category:** Rails\
**Created:** [October 26, 2009, 11:26pm UTC](https://www.ruby-forum.com/t/self-and-protected-methods-question-confusion/177942 "2009-10-26T23:26:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cswebgrl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cswebgrl](https://www.ruby-forum.com/u/cswebgrl)\
**Post date:** [October 26, 2009, 11:26pm UTC](https://www.ruby-forum.com/t/self-and-protected-methods-question-confusion/177942/1 "2009-10-26T23:26:10Z")

</div>

Hi,

I’m still trying to learn the in’s and out’s of Ruby and Rails. I’ve  
been testing my code a million different ways and have read a number of  
books and web sites, but something is still not clicking for me. I want  
to be able to call the protected methods and I have read both David  
Black’s and Prag Programmers books about how self can be tricky. But I  
still don’t know how to make it work. I want to call method\_a and have  
it then process both protected methods.

Is it possible to call my method\_a directly (as shown in my script code)  
without having to do this:

k = Klass.new  
k.method\_a

* * *

I’ve got this right now.

This is called from a script.  
k = Klass.method\_a(:variable = v)

My model:  
class Klass

def self.method\_a(params = {})  
a = Model.find(:first, :conditions =\> [“variable = ?”, variable])  
a.do\_something  
a.do\_more  
return a  
end

protected

def do\_something  
puts “inside do\_something”  
end

def do\_more  
puts “inside do\_more”  
end

end

Thanks for helping a Ruby N…

---

<div class="post-metadata">

**Author:** ![cswebgrl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cswebgrl](https://www.ruby-forum.com/u/cswebgrl)\
**Post date:** [October 27, 2009, 12:34am UTC](https://www.ruby-forum.com/t/self-and-protected-methods-question-confusion/177942/2 "2009-10-27T00:34:43Z")

</div>

Greg D. wrote:

> Here’s an example:
> 
> #!/usr/bin/env ruby
> 
> class Foo  
> private  
> def hidden  
> puts “I am hidden”  
> end  
> end
> 
> f = Foo.new
> 
> begin  
> f.hidden  
> rescue  
> puts “Can’t run hidden: #{ $! }”  
> end
> 
> f.send( :hidden )
> 
> > ./private.rb
> 
> Can’t run hidden: private method `hidden’ called for #Foo:0xb7c2f92c  
> I am hidden

Thank you for your example. I was able to test my code and call my  
methods using object.send (:method)

My follow-up question to this is now, is this the best way to handle the  
protected methods using the code sample that I have above? Or do I have  
a design flaw in the code? Should I explicitly create and call  
object.New and the method instead of creating the object like I am  
doing?

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![cswebgrl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@cswebgrl](https://www.ruby-forum.com/u/cswebgrl)\
**Post date:** [October 27, 2009, 12:15am UTC](https://www.ruby-forum.com/t/self-and-protected-methods-question-confusion/177942/3 "2009-10-27T00:15:34Z")

</div>

On Mon, Oct 26, 2009 at 5:26 PM, Cs Webgrl  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m still trying to learn the in’s and out’s of Ruby and Rails. I’ve  
> been testing my code a million different ways and have read a number of  
> books and web sites, but something is still not clicking for me. I want  
> to be able to call the protected methods

Ruby’s object system is a message passing system. If you have an  
object foo, and you do:

foo.some\_method

foo doesn’t perform a method call on itself. foo just looks for  
somewhere to send the “some\_method” message, somewhere that returns  
true for respond\_to?

foo.respond\_to?(:some\_message)

In knowing how this works, you can send the message yourself, ignoring  
the private or protected nature of the receiver along the way.

Here’s an example:

#!/usr/bin/env ruby

class Foo  
private  
def hidden  
puts “I am hidden”  
end  
end

f = Foo.new

begin  
f.hidden  
rescue  
puts “Can’t run hidden: #{ $! }”  
end

f.send( :hidden )

> ./private.rb

Can’t run hidden: private method `hidden’ called for #Foo:0xb7c2f92c  
I am hidden

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)
