# Calling methods from within singleton class

**URL:** <https://www.ruby-forum.com/t/calling-methods-from-within-singleton-class/77243>\
**Category:** Ruby\
**Created:** [November 25, 2006, 12:08pm UTC](https://www.ruby-forum.com/t/calling-methods-from-within-singleton-class/77243 "2006-11-25T12:08:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [November 25, 2006, 12:08pm UTC](https://www.ruby-forum.com/t/calling-methods-from-within-singleton-class/77243/1 "2006-11-25T12:08:51Z")

</div>

I’d like some clarification on the following simple examples.

class Foo  
self  
end # Foo

class \<\< Foo  
self  
end # #Class:Foo

No surprise there. In the first case I’m defining the class and  
returning self, which of course is the class, and in the second it’s  
the singleton class.

class \<\< Foo  
def foo  
“foo”  
end  
end

Now I can call Foo.foo as expected. It could have just as easily been  
def Foo.foo  
“foo”  
end

However, I can’t do  
class \<\< Foo  
foo  
end

to call it. It results in “NameError: undefined local variable or  
method `foo’ for #Class:Foo”. What’s the difference?

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [November 26, 2006, 1:38pm UTC](https://www.ruby-forum.com/t/calling-methods-from-within-singleton-class/77243/2 "2006-11-26T13:38:43Z")

</div>

On 11/25/06, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class \<\< Foo  
> def foo  
> “foo”  
> end  
> end

This defines a `foo’ method on instances of Foo’s singleton class,  
namely Foo.

> class \<\< Foo  
> foo  
> end
> 
> … NameError …

This calls `foo’ on Foo’s singleton class, not Foo.

Note that “#Class:Foo” is #inspect-ese for “Foo’s singleton class”,  
not “Foo”… 😉

G.
