# Can't I call a private method from a self.method

**URL:** <https://www.ruby-forum.com/t/cant-i-call-a-private-method-from-a-self-method/148287>\
**Category:** Rails\
**Created:** [September 11, 2008, 10:34am UTC](https://www.ruby-forum.com/t/cant-i-call-a-private-method-from-a-self-method/148287 "2008-09-11T10:34:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 11, 2008, 10:34am UTC](https://www.ruby-forum.com/t/cant-i-call-a-private-method-from-a-self-method/148287/1 "2008-09-11T10:34:25Z")

</div>

Hi  
I have in MailSending class  
def self.send\_mail\_to\_all\_contacts(problem)  
description=split\_name\_and\_email\_array(joined\_array,problem)  
end

private  
def split\_name\_and\_email\_array(name\_email\_array,pb)  
code…  
split\_value=“somevaluehere”  
end

But here I get error as  
undefined method `split\_name\_and\_email\_array’ for MailSending:Class  
So Can’t I call a private method from a class method

Thanks in advance  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 11, 2008, 10:52am UTC](https://www.ruby-forum.com/t/cant-i-call-a-private-method-from-a-self-method/148287/2 "2008-09-11T10:52:12Z")

</div>

On Sep 11, 9:34 am, Sijo Kg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> But here I get error as  
> undefined method `split\_name\_and\_email\_array’ for MailSending:Class  
> So Can’t I call a private method from a class method

You can’t but that isn’t the problem here. You can’t call an instance  
method without an instance of the class (which you don’t have).

Fred

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 11, 2008, 10:56am UTC](https://www.ruby-forum.com/t/cant-i-call-a-private-method-from-a-self-method/148287/3 "2008-09-11T10:56:29Z")

</div>

Hi  
Thanks for the reply…I can call the first like  
MailSendingProblem.send\_mail\_to\_all\_contacts(@problem) upto that it is  
Ok But it does not call the private method So need I to make the above  
private method public or any other solution?

Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 11, 2008, 11:42am UTC](https://www.ruby-forum.com/t/cant-i-call-a-private-method-from-a-self-method/148287/4 "2008-09-11T11:42:05Z")

</div>

On 11 Sep 2008, at 09:56, Sijo Kg wrote:

> Hi  
> Thanks for the reply…I can call the first like  
> MailSendingProblem.send\_mail\_to\_all\_contacts(@problem) upto that it  
> is  
> Ok But it does not call the private method So need I to make the above  
> private method public or any other solution?

Like I said you can’t call an instance method without an instance.  
It’s like trying to say ActiveRecord::Base.reload - it doesn’t make  
any sense - you can only call that on an instance.

Fred

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 12, 2008, 4:13am UTC](https://www.ruby-forum.com/t/cant-i-call-a-private-method-from-a-self-method/148287/5 "2008-09-12T04:13:36Z")

</div>

You need to declare your “split\_name\_and\_email\_array” method as a class  
method:

def self.split\_name\_and\_email\_array(name\_email\_array,pb)

# …

end

Christian

On Thu, Sep 11, 2008 at 10:34 AM, Sijo Kg

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 11, 2008, 1:02pm UTC](https://www.ruby-forum.com/t/cant-i-call-a-private-method-from-a-self-method/148287/6 "2008-09-11T13:02:58Z")

</div>

Frederick C. wrote:

> Like I said you can’t call an instance method without an instance.  
> It’s like trying to say ActiveRecord::Base.reload - it doesn’t make  
> any sense - you can only call that on an instance.
> 
> Fred

# Ok, so would it be like: self.method() or obj=MailSending.new() and then obj.method()? Also in the book I’m reading at the moment says that to use “before filter” to intercept to the actions in the controller we have to put the particular method in ApplicationController(the parent class of all our controllers) and that we have to restrict it as any public methods here are exposed to end users as actions and hence asks to make it private:

# class ApplicationController \< ActionController::Base private def authorize … end end

# and in the corresponding controllers call it with:

# class SomeController \< ApplicationController before\_filter :authorize end

Problems:  
Though I have called the method accordingly in the corresponding  
controllers, as soon as I make the authorize method private a warning  
icon appears at the side as “unused private method authorize” and  
underlines all the words with curly yellow lines (by the way I’m using  
aptana studio). But, however, everything seems to work fine. So, how can  
I get rid of those warning and ugly underlines. Thanks…
