# Calling class method from instance method

**URL:** <https://rubytalk.org/t/calling-class-method-from-instance-method/45716>\
**Category:** ruby-talk\
**Created:** [7 April 2008 08:58 UTC](https://rubytalk.org/t/calling-class-method-from-instance-method/45716 "2008-04-07T08:58:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yu-shan\_Fung](https://avatars.discourse-cdn.com/v4/letter/y/8797f3/32.png) [@Yu-shan\_Fung](https://rubytalk.org/u/Yu-shan_Fung)\
**Post date:** [7 April 2008 08:58 UTC](https://rubytalk.org/t/calling-class-method-from-instance-method/45716/1 "2008-04-07T08:58:44Z")

</div>

Pretty dumb question -- how do I call a class method from inside an  
instance method (of the same class)?

The use case I have is that in an activerecord model class, I would like  
to define an instance method which calls find\_by\_sql.

I have tried and found that this works:  
&nbsp;&nbsp;self.class.find\_by\_sql

But this seems pretty verbose for something so obvious.

Thanks!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [7 April 2008 09:11 UTC](https://rubytalk.org/t/calling-class-method-from-instance-method/45716/2 "2008-04-07T09:11:33Z")

</div>

You can replace self.class with the name of the class. For example:

class C  
&nbsp;&nbsp;  
&nbsp;&nbsp;def C.x  
&nbsp;&nbsp;end

&nbsp;&nbsp;def y  
&nbsp;&nbsp;&nbsp;&nbsp;self.class.x  
&nbsp;&nbsp;&nbsp;&nbsp;C.x  
&nbsp;&nbsp;end

end

There's a difference, however. If you derive a class from C and override C.x  
from D:

class D \< C  
&nbsp;&nbsp;def self.x  
&nbsp;&nbsp;end  
end

using self.class.x from C#y will use the version of x defined in the  
instance's class (that is, C.x if called by an instance of C and D.x if called  
by an instance of D). Using C.x will, obviously, always call C.x. Here's some  
code which demonstrates this:

class C  
&nbsp;&nbsp;def self.x  
&nbsp;&nbsp;&nbsp;&nbsp;puts "x of C"  
&nbsp;&nbsp;end

&nbsp;&nbsp;def y  
&nbsp;&nbsp;&nbsp;&nbsp;self.class.x  
&nbsp;&nbsp;end

&nbsp;&nbsp;def z  
&nbsp;&nbsp;&nbsp;&nbsp;C.x  
&nbsp;&nbsp;end  
end

class D \< C  
&nbsp;&nbsp;def self.x  
&nbsp;&nbsp;&nbsp;&nbsp;puts "x of D"  
&nbsp;&nbsp;end  
end

puts "Calling y on an instance of C"  
C.new.y  
puts "Calling y on an instance of D"  
D.new.y  
puts "Calling z on an instance of D"  
D.new.z

I hope this helps

Stefano

> **···**
>
> On Monday 07 April 2008, Yu-shan Fung wrote:
> 
> > Pretty dumb question -- how do I call a class method from inside an  
> > instance method (of the same class)?
> > 
> > The use case I have is that in an activerecord model class, I would like  
> > to define an instance method which calls find\_by\_sql.
> > 
> > I have tried and found that this works:  
> > &nbsp;&nbsp;self.class.find\_by\_sql
> > 
> > But this seems pretty verbose for something so obvious.
> > 
> > Thanks!

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [7 April 2008 09:12 UTC](https://rubytalk.org/t/calling-class-method-from-instance-method/45716/3 "2008-04-07T09:12:57Z")

</div>

Hi --

> **···**
>
> On Mon, 7 Apr 2008, Yu-shan Fung wrote:
> 
> > Pretty dumb question -- how do I call a class method from inside an  
> > instance method (of the same class)?
> > 
> > The use case I have is that in an activerecord model class, I would like  
> > to define an instance method which calls find\_by\_sql.
> > 
> > I have tried and found that this works:  
> > self.class.find\_by\_sql
> > 
> > But this seems pretty verbose for something so obvious.
> 
> You need the 'self.' part because class is a keyword. It's a bit  
> verbose but something's got to give 🙂
> 
> David
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS April 14-17 New York City  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 9-12 Berlin  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS June 16-19 Berlin  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![marco5](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@marco5](https://rubytalk.org/u/marco5)\
**Post date:** [23 February 2011 01:44 UTC](https://rubytalk.org/t/calling-class-method-from-instance-method/45716/4 "2011-02-23T01:44:02Z")

</div>

Is it possible to do the opposite? call a instance method from a class  
method?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![marco5](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@marco5](https://rubytalk.org/u/marco5)\
**Post date:** [23 February 2011 12:40 UTC](https://rubytalk.org/t/calling-class-method-from-instance-method/45716/5 "2011-02-23T12:40:51Z")

</div>

Ryan Davis ,  
"When you want to have a class call an "instance method", which instance  
would call it?"

Good point! Makes sense.

Anurag Priyam, this is pretty wierd... but works.

You see, there's stuff that need to be donne in both cases, in instances  
and in classes. For example:

class SomeClass  
&nbsp;&nbsp;def self.method  
&nbsp;&nbsp;&nbsp;&nbsp;if file\_exists?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#do stuff  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;#something need to be donne here, and I need to check if the file  
exists  
&nbsp;&nbsp;&nbsp;&nbsp;#I dont want to define a nother class method exactly the same as the  
already declared instance method.  
&nbsp;&nbsp;end

&nbsp;&nbsp;def file\_exists?  
&nbsp;&nbsp;&nbsp;&nbsp;#there are instance methods depending on this one  
&nbsp;&nbsp;&nbsp;&nbsp;File.exists?("some\_file\_name.txt")  
&nbsp;&nbsp;end  
end

How do you deal with this kind of thing?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![marco5](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@marco5](https://rubytalk.org/u/marco5)\
**Post date:** [6 March 2011 16:12 UTC](https://rubytalk.org/t/calling-class-method-from-instance-method/45716/6 "2011-03-06T16:12:45Z")

</div>

Thanks for the replies.

Turned out it was a matter of code organization. The methods I needed to  
call, I put in another class, and called from its instances.

regards

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [23 February 2011 04:16 UTC](https://rubytalk.org/t/calling-class-method-from-instance-method/45716/7 "2011-02-23T04:16:48Z")

</div>

When you have an instance "call a class method" you're really having the instance ask the class (an instance of Class) to call it's method.

When you want to have a class call an "instance method", which instance would call it?

> **···**
>
> On Feb 22, 2011, at 17:44 , marco antonio f. wrote:
> 
> > Is it possible to do the opposite? call a instance method from a class  
> > method?

---

<div class="post-metadata">

**Author:** ![Anurag\_Priyam](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@Anurag\_Priyam](https://rubytalk.org/u/Anurag_Priyam)\
**Post date:** [23 February 2011 06:46 UTC](https://rubytalk.org/t/calling-class-method-from-instance-method/45716/8 "2011-02-23T06:46:58Z")

</div>

> Is it possible to do the opposite? call a instance method from a class  
> method?

Create an instance and call the instance method; very weird usage  
though. Why would you need it?

class Foo  
&nbsp;&nbsp;def self.hello  
&nbsp;&nbsp;&nbsp;&nbsp;Foo.new.hello  
&nbsp;&nbsp;end

&nbsp;&nbsp;def hello  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Hello World"  
&nbsp;&nbsp;end  
end

You can basically call any method from anywhere if you have the right receiver.

> **···**
>
> --  
> Anurag Priyam  
> [http://about.me/yeban/](http://about.me/yeban/)

---

<div class="post-metadata">

**Author:** ![John\_Feminella](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@John\_Feminella](https://rubytalk.org/u/John_Feminella)\
**Post date:** [23 February 2011 12:55 UTC](https://rubytalk.org/t/calling-class-method-from-instance-method/45716/9 "2011-02-23T12:55:37Z")

</div>

First, write your class method so that it accepts an instance:

==== begin snippet ====  
class ExtendedFile  
&nbsp;&nbsp;attr\_accessor :filename

&nbsp;&nbsp;def self.process(file)  
&nbsp;&nbsp;&nbsp;&nbsp;if file.exists?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do some work  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;def exists?  
&nbsp;&nbsp;&nbsp;&nbsp;self.exists? self.filename  
&nbsp;&nbsp;end  
end  
==== end snippet ====

Of course, once you can do that, then you can refactor the class  
method into an instance method. Delete the method's argument and  
replace it with a reference to `self`:

==== begin snippet ====  
class ExtendedFile  
&nbsp;&nbsp;attr\_accessor :filename

&nbsp;&nbsp;def process # now it's an instance method  
&nbsp;&nbsp;&nbsp;&nbsp;if self.exists? # reference self instead of the argument  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do some work  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;def exists?  
&nbsp;&nbsp;&nbsp;&nbsp;self.exists? self.filename  
&nbsp;&nbsp;end  
end  
==== end snippet ====

~ jf

> **···**
>
> --  
> John Feminella  
> Principal Consultant, BitsBuilder  
> LI: [http://www.linkedin.com/in/johnxf](http://www.linkedin.com/in/johnxf)  
> SO: [User John Feminella - Stack Overflow](http://stackoverflow.com/users/75170/)
> 
> On Wed, Feb 23, 2011 at 07:40, marco antonio f. \<marcofognog@gmail.com\> wrote:
> 
> > Ryan Davis ,  
> > "When you want to have a class call an "instance method", which instance  
> > would call it?"
> > 
> > Good point! Makes sense.
> > 
> > Anurag Priyam, this is pretty wierd... but works.
> > 
> > You see, there's stuff that need to be donne in both cases, in instances  
> > and in classes. For example:
> > 
> > class SomeClass  
> > def self.method  
> > if file\_exists?  
> > #do stuff  
> > end  
> > #something need to be donne here, and I need to check if the file  
> > exists  
> > #I dont want to define a nother class method exactly the same as the  
> > already declared instance method.  
> > end
> > 
> > def file\_exists?  
> > #there are instance methods depending on this one  
> > File.exists?("some\_file\_name.txt")  
> > end  
> > end
> > 
> > How do you deal with this kind of thing?
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Anurag\_Priyam](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@Anurag\_Priyam](https://rubytalk.org/u/Anurag_Priyam)\
**Post date:** [23 February 2011 14:09 UTC](https://rubytalk.org/t/calling-class-method-from-instance-method/45716/10 "2011-02-23T14:09:48Z")

</div>

> You see, there's stuff that need to be donne in both cases, in instances  
> and in classes. For example:
> 
> class SomeClass  
> def self.method  
> if file\_exists?  
> #do stuff  
> end  
> #something need to be donne here, and I need to check if the file  
> exists  
> #I dont want to define a nother class method exactly the same as the  
> already declared instance method.  
> end

Since file\_exists? is an instance method, it is very likely to return  
true/false based on the state of the instance (see below). Calling  
file\_exists in self.method does not make sense unless its  
responsibility is to act on an instance of your class - like a  
factory, or processing an instance passed to it as a parameter.

def self.method(object)  
&nbsp;&nbsp;if object.file\_exists?  
&nbsp;&nbsp;&nbsp;&nbsp;# do something on that object  
&nbsp;&nbsp;&nbsp;&nbsp;object.statistics  
&nbsp;&nbsp;end  
end

or,

def self.create(\*args)  
&nbsp;&nbsp;object = SomeClass.new(\*args)  
&nbsp;&nbsp;return object if object.file\_exists?  
end

> def file\_exists?  
> #there are instance methods depending on this one  
> File.exists?("some\_file\_name.txt")  
> end

Umm, is the file name hard coded, or does it depend on the state of  
the instance, like:

def file\_exists?  
&nbsp;&nbsp;File.exists?(@file)  
end

If it is hard coded, or depends on a constant, you might as well  
define a class method:

class SomeClass  
&nbsp;&nbsp;CONFIG = "~/.foo.rc"

&nbsp;&nbsp;def self.config\_exists?  
&nbsp;&nbsp;&nbsp;&nbsp;File.exists?(CONFIG)  
&nbsp;&nbsp;end

&nbsp;&nbsp;def init  
&nbsp;&nbsp;&nbsp;&nbsp;raise "Config file not found" unless SomeClass.config\_exists?  
&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;end  
end

> end

> How do you deal with this kind of thing?

Depends on the context.

> **···**
>
> --  
> Anurag Priyam  
> [http://about.me/yeban/](http://about.me/yeban/)
