# Preferred way to call a class method?

**URL:** <https://www.ruby-forum.com/t/preferred-way-to-call-a-class-method/72165>\
**Category:** Ruby\
**Created:** [October 15, 2006, 2:27am UTC](https://www.ruby-forum.com/t/preferred-way-to-call-a-class-method/72165 "2006-10-15T02:27:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pachl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@pachl](https://www.ruby-forum.com/u/pachl)\
**Post date:** [October 15, 2006, 2:27am UTC](https://www.ruby-forum.com/t/preferred-way-to-call-a-class-method/72165/1 "2006-10-15T02:27:15Z")

</div>

class Test  
def self.class\_meth  
‘inside class method’  
end

def inst\_meth1  
self.class.class\_meth  
end

def inst\_meth2  
Test.class\_meth  
end  
end

What is the preferred way of calling a class method from within an  
instance method, . or self.class.?

What are the side effects to either method?

-pachl

---

<div class="post-metadata">

**Author:** ![pachl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5f9b8f/32.png) [@pachl](https://www.ruby-forum.com/u/pachl)\
**Post date:** [October 15, 2006, 3:05am UTC](https://www.ruby-forum.com/t/preferred-way-to-call-a-class-method/72165/2 "2006-10-15T03:05:18Z")

</div>

On 10/14/06, Clint P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> Test.class_meth
> 
> ```
> 
> end  
> end
> 
> What is the preferred way of calling a class method from within an  
> instance method, . or self.class.?

In the first case the class\_meth will be sent to the receivers class,  
while in the second it will always be sent to Test.

If there are subclasses of Test there can be a difference

class Test

```
def self.class_meth
     puts "Test's class method called"
end

```

def inst\_meth1  
self.class.class\_meth  
end

def inst\_meth2  
Test.class\_meth  
end

end

class Test1 \< Test  
def self.class\_meth  
puts “Test1’s class method called”  
end

end

irb(main):023:0\> Test1.new.inst\_meth1  
Test1’s class method called  
=\> nil  
irb(main):024:0\> Test1.new.inst\_meth2  
Test’s class method called

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
