Forum: Ruby class << self ...end - how does this create class methods?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-05-31 21:18
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
D4e51fd9554030ab55c379fdc1a34826?d=identicon&s=25 Keith Lancaster (klancaster)
on 2006-05-31 21:35
(Received via mailing list)
On 5/31/06, Wes Gamble <weyus@att.net> wrote:
>
>
> --
> Posted via http://www.ruby-forum.com/.


Here's an excerpt from Ruby for Rails by Black that does a good job of
explaining it...
<begin excerpt>
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.
 <end excerpt>

HTH,
Keith
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-05-31 21:49
Keith,

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

Powerful magic.

Wes
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 MenTaLguY (Guest)
on 2006-05-31 22:08
(Received via mailing list)
On Thu, 1 Jun 2006 04:18:18 +0900, Wes Gamble <weyus@att.net> 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

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

  foo = Object.new

  def foo.blah
    puts "eek"
  end

  foo.blah # => eek

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

   foo = Object.new

   class << foo
     def blah
       puts "eek"
     end
   end

   foo.blah # => eek

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

  class Foo
    p self # => Foo
  end

 5. "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
397f61cc4458e13157b4facf72325e5f?d=identicon&s=25 Gennady Bystritsky (Guest)
on 2006-05-31 22:09
(Received via mailing list)
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.
This topic is locked and can not be replied to.