# How to call a class method via the singleton if it exists

**URL:** <https://www.ruby-forum.com/t/how-to-call-a-class-method-via-the-singleton-if-it-exists/76416>\
**Category:** Ruby\
**Created:** [November 18, 2006, 5:32pm UTC](https://www.ruby-forum.com/t/how-to-call-a-class-method-via-the-singleton-if-it-exists/76416 "2006-11-18T17:32:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [November 18, 2006, 5:32pm UTC](https://www.ruby-forum.com/t/how-to-call-a-class-method-via-the-singleton-if-it-exists/76416/1 "2006-11-18T17:32:53Z")

</div>

I want to call a class level method from the the instance level, but I  
need it to go through the singeton if it exists. Here is the most basic  
example:

class X  
def self.f ; “A” ; end  
def g ; self.class.f ; end  
end

x = X.new  
x.g #=\> “A”

def x.f; “B”; end  
x.g #=\> “A”

How do I get the last to return “B”? I know I could use  
‘(class\<\<self;self;end)’ instead of ‘self.class’, but that will create  
a singelton even if doesn’t exist, which seems very wasteful. Yet I  
don’t recall any (non-hack) way to detect if a singleton is defined. Is  
there?

It would be nice if there was a reference with which we could call up  
through the class level like this.

T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [November 19, 2006, 12:09am UTC](https://www.ruby-forum.com/t/how-to-call-a-class-method-via-the-singleton-if-it-exists/76416/2 "2006-11-19T00:09:38Z")

</div>

Hi,

At Sun, 19 Nov 2006 01:00:15 +0900,  
Trans wrote in [ruby-talk:225642]:

> I want to call a class level method from the the instance level, but I  
> need it to go through the singeton if it exists. Here is the most basic  
> example:

If I were you:

> class X  
> def self.f ; “A” ; end  
> def f; self.class.f; end  
> def g ; f ; end
