# Calling class method from instance

**URL:** <https://www.ruby-forum.com/t/calling-class-method-from-instance/159979>\
**Category:** Ruby\
**Created:** [February 10, 2009, 5:21pm UTC](https://www.ruby-forum.com/t/calling-class-method-from-instance/159979 "2009-02-10T17:21:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Larz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/85f322/32.png) [@Larz](https://www.ruby-forum.com/u/Larz)\
**Post date:** [February 10, 2009, 5:21pm UTC](https://www.ruby-forum.com/t/calling-class-method-from-instance/159979/1 "2009-02-10T17:21:08Z")

</div>

I was puzzled on this but figured out a way to do it. Figured I’d post  
it on here, maybe there’s a better way …

class Test

@c = 2  
def self.ctest  
puts @c  
end

def initialize(i)  
@i = i  
end

def itest  
puts @i

# here’s what I was trying to do !!!

puts self.class.ctest  
end

end

ob = Test.new(7)

ob.itest  
Test.ctest

---

<div class="post-metadata">

**Author:** ![Larz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/85f322/32.png) [@Larz](https://www.ruby-forum.com/u/Larz)\
**Post date:** [February 10, 2009, 8:17pm UTC](https://www.ruby-forum.com/t/calling-class-method-from-instance/159979/2 "2009-02-10T20:17:02Z")

</div>

On Tue, Feb 10, 2009 at 5:19 PM, Larz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> puts self.class.ctest  
> IMHO this call syntax is optimal, because agnostic to the class name.  
> Robert

---

<div class="post-metadata">

**Author:** ![Larz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/85f322/32.png) [@Larz](https://www.ruby-forum.com/u/Larz)\
**Post date:** [February 10, 2009, 9:28pm UTC](https://www.ruby-forum.com/t/calling-class-method-from-instance/159979/3 "2009-02-10T21:28:48Z")

</div>

Larz wrote:

> puts self.class.ctest

What’s the reason for not simply writing your class like this:

class Test  
@c = 20

def Test.a  
puts @c  
end

def b  
Test.a  
end

end

t = Test.new  
t.b

–output:–  
20

---

<div class="post-metadata">

**Author:** ![Larz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/85f322/32.png) [@Larz](https://www.ruby-forum.com/u/Larz)\
**Post date:** [February 10, 2009, 11:24pm UTC](https://www.ruby-forum.com/t/calling-class-method-from-instance/159979/4 "2009-02-10T23:24:17Z")

</div>

On Tue, Feb 10, 2009 at 3:27 PM, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> def Test.a  
> t.b
> 
> ## –output:– 20

That’s fine unless you want to, say, override the class method in a  
subclass

class TestSub \< Test

def TestSub.a  
puts “doin my own thang.”  
end  
end

## t = TestSub.new t.b –output is still– 20

Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)

---

<div class="post-metadata">

**Author:** ![Larz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/85f322/32.png) [@Larz](https://www.ruby-forum.com/u/Larz)\
**Post date:** [February 11, 2009, 1:05am UTC](https://www.ruby-forum.com/t/calling-class-method-from-instance/159979/5 "2009-02-11T01:05:03Z")

</div>

On Tue, Feb 10, 2009 at 11:21 PM, Rick DeNatale  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That’s fine unless you want to, say, override the class method in a subclass  
> Good point, but there is another issue here.  
> Your project guru comes along and tells you: Nice code, but we should  
> not use the name Test (for a dumb reason of course).  
> Now you have to change Test to Check in your source  
> would you prefer to change it here

class Test  
def a # multiply this with n entries  
self.class.x  
end  
def self.a  
…

or here

class Test  
def a  
Test.x  
end  
def Test.a

end

In other words the second version is not DRY and the worst penalty for  
unDRYness is the need to change your code.

Robert

---

<div class="post-metadata">

**Author:** ![Larz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/85f322/32.png) [@Larz](https://www.ruby-forum.com/u/Larz)\
**Post date:** [February 11, 2009, 2:04am UTC](https://www.ruby-forum.com/t/calling-class-method-from-instance/159979/6 "2009-02-11T02:04:10Z")

</div>

Larz wrote:

> def itest  
> puts @i
> 
> # here’s what I was trying to do !!!
> 
> puts self.class.ctest  
> end

I think Object#class is something to be avoided in most cases. Frankly  
it’s none of your business what class was used to create an object. It  
doesn’t matter _how_ an object sprang into existence, what matters is  
how it quacks.

I want to replace an object with a delegate with no ill effects. Or  
with a mock object, or with whatever. But uses of Object#class defeat  
this.

I would be inclined to make shared data explicit either with a constant  
or a with a passed-in reference to the shared data, rather than implicit  
with Object#class.

class Test  
SHARED = Struct.new(:ctest).new(2)

def itest  
puts SHARED.ctest  
end  
end

Test.new.itest #=\> 2
