# Class \<\< self ...end - how does this create class methods?

**URL:** <https://www.ruby-forum.com/t/class-self-end-how-does-this-create-class-methods/95420>\
**Category:** Ruby\
**Created:** [May 31, 2006, 9:18pm UTC](https://www.ruby-forum.com/t/class-self-end-how-does-this-create-class-methods/95420 "2006-05-31T21:18:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [May 31, 2006, 9:18pm UTC](https://www.ruby-forum.com/t/class-self-end-how-does-this-create-class-methods/95420/1 "2006-05-31T21:18:07Z")

</div>

I just ran into a seemingly weird problem until I discovered that the  
method that I kept trying to call as an instance method was in fact a  
class method. But of course, there isn’t anything in the signature to  
indicate that to me. Apparently, if you do this:

class \<\< self  
def x  
end  
end

then x is a class method, not an instance method.

I’m having trouble finding a good explanation of this in the Pickaxe  
book - is someone willing to give me the low down.

This appears to be the common idiom for defining class methods and I’d  
like to understand so I don’t waste any more time.

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [May 31, 2006, 9:49pm UTC](https://www.ruby-forum.com/t/class-self-end-how-does-this-create-class-methods/95420/2 "2006-05-31T21:49:01Z")

</div>

Keith,

Thanks - I haven’t gotten to this section of the book yet :). It makes  
some sense.

Powerful magic.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [May 31, 2006, 9:35pm UTC](https://www.ruby-forum.com/t/class-self-end-how-does-this-create-class-methods/95420/3 "2006-05-31T21:35:58Z")

</div>

On 5/31/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

Here’s an excerpt from Ruby for Rails by Black that does a good job of  
explaining it…  
  
Defining class methods with class \<\<

By far the most frequent use of the class \<\< notation for entering a  
singleton method  
class is in connection with class method definitions. You’ll see this  
quite  
often:  
class Ticket  
class \<\< self  
def most\_expensive(tickets)

# etc.

This code results in a class method Ticket.most\_expensive. That method  
could

also be defined like this (assuming this code comes at a point in the  
program  
where the Ticket class already exists):  
class \<\< Ticket  
def most\_expensive(tickets)

# etc.

Because self is Ticket inside the class Ticket definition body, class \<\<  
self  
inside the body is the same as class \<\< Ticket outside the body.  
(Technically, you  
could do class \<\< Ticket even inside the body of class Ticket, but in  
practice  
you’ll usually see class \<\< self whenever the object whose singleton  
class  
needs  
opening is self.)  
The fact that class \<\< self shows up frequently in connection with the  
crea-  
tion of class methods sometimes leads to the false impression that the  
class  
\<\<  
notation can only be used to create class methods, or that the only  
expression you  
can legally put on the right is self. In fact, class \<\< self inside a  
class  
definition  
block is just one case of the class \<\< object notation. The technique is  
general: It  
puts you in a definition block for the singleton class of object,  
whatever  
object  
may be. That, in turn, means you’re operating in a context where  
whatever  
you  
doâ??whatever you add to the classâ??pertains only to that one object.

HTH,  
Keith

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [May 31, 2006, 10:09pm UTC](https://www.ruby-forum.com/t/class-self-end-how-does-this-create-class-methods/95420/4 "2006-05-31T22:09:08Z")

</div>

Pickaxe2 in chapter 24 on page 362 (“Classes and Objects”) goes into  
great extend explaining the “class \<\< object” feature, with “class \<\<  
self” in particular. With pictures and good examples. It may be a good  
starting point.

Hope it helps,  
Gennady.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [May 31, 2006, 10:08pm UTC](https://www.ruby-forum.com/t/class-self-end-how-does-this-create-class-methods/95420/5 "2006-05-31T22:08:50Z")

</div>

On Thu, 1 Jun 2006 04:18:18 +0900, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> then x is a class method, not an instance method.  
> There are several different pieces which fit together:

1. Classes are themselves represented as objects.

class Foo  
# …  
end

p Foo.class # =\> Class

1. Methods can be defined on a per-object basis

foo = Object.new

def foo.blah  
puts “eek”  
end

foo.blah # =\> eek

1. you can also define per-object methods this way:

foo = Object.new

class \<\< foo  
def blah  
puts “eek”  
end  
end

foo.blah # =\> eek

1. outside of methods, within a class … end block, self refers to the  
class

class Foo  
p self # =\> Foo  
end

1. “class methods” are simply per-object methods defined on classes

class Foo  
class \<\< self  
def blah  
puts “eek”  
end  
end  
end

Foo.blah # =\> eek

-mental
