views:

86

answers:

5

Possible Duplicate:
class << self idiom in Ruby

I have a quick Ruby question. I come from a Java/c background, so I understand in Ruby "self" when referenced inside a instance method acts like "this". And "self." prefix for method defines it as a class method.

But what does this mean here??

class << self
  def some_method
  end
end
+4  A: 

What's happening here is that we're reopening an object's class from within itself and defining a new instance method on it. This is one of the ways you can do so-called "monkey-patching" in Ruby. This method adds the method only to the current object rather than all objects of the class.

It's equivalent to doing this:

my_obj = MyClass.new

class << my_obj
  def some_method
  end
end

# or...

def my_obj.some_method
end

Here's a good article that covers it: Learning Ruby: class << self.

Jordan
What current object?
OscarRyz
OscarRyz: The instance within which the code is called.
Jordan
I think this answer should talk more explicitly about the singleton class or eigenclass. "reopening an object's class from within itself" is too vague and the word 'class' as used doesn't make it clear that the class is the singleton class, and not just the class returned by going `obj.class`
banister
A: 

If you use it like this:

class Foo
    class << self
        def something
            puts "something"
        end
        def something_else
            puts "something else"
        end
    end
end

Foo.something

It's a shorthand for:

class Foo
    def self.something
        puts "something"
    end
    def self.something_else
        puts "something else"
    end
end

I'm not a fan of the notation, really.

it's not a 'shorthand' at all. If anything your latter syntax is a kind of shorthand for the former. `class << self` opens the eigenclass of an object and can be used for a lot more than simply defining methods.
banister
It's only shorthand if you're adding a LOT of class variables. I left that observation as an exercise for the reader, figuring that they'd get the idea that it's not something they should be doing unless they have a damn good reason. And they don't. =]
A: 

This is how you define methods for an object's singleton/eigen class.

class << foo
  def bar
  end
end

is equivalent to

def foo.bar
end
Bayard Randel
A: 

Two concepts to remember:

  1. Every class is also an object.
  2. Every object has a singleton class (called a meta-class).

Some example code:

class A
  B = "Hello World!"
  def self.a
    puts B
  end

  def A.b
    puts B
  end

  class << self
    B = "B has changed"
    def c
      puts B
    end
  end
end

>> A.a
Hello World!
=> nil
>> A.b
Hello World!
=> nil
>> A.c
B has changed
=> nil
dbyrne
+1  A: 

The syntax class << some_objct opens up some_object's singleton class, which is a special "secret" class that only that object belongs to. Using the singleton class, you can define methods one object responds to while other instances of its normal class do not.

So, for example:

a = "Hello world"
b = "Hello world" # Note that this is a different String object

class << a
  def first_letter
    self[0,1]
  end
end

puts a.first_letter # prints "H"
puts b.first_letter # Raises an error because b doesn't have that method

In a class context, these two method definitions are equivalent:

class Foo
  def self.bar
    puts "Yo dawg"
  end
end

class Foo
  class << self
    def bar
      puts "Yo dawg"
    end
  end
end

The second form can be useful in certain circumstances (such as when you want to declare attr_accessors for the class object itself).

Chuck