78
class << Awesomeness

What is this << for? I searched, but the results only tell me about string concatenation...

Dennis Hackethal
  • 13,662
  • 12
  • 66
  • 115
Saturn
  • 17,888
  • 49
  • 145
  • 271

3 Answers3

141

While it's true that class << something is the syntax for a singleton class, as someone else said, it's most often used to define class methods within a class definition. But these two usages are consistent. Here's how.

Ruby lets you add methods to any particular instance by doing this:

class << someinstance
  def foo
    "Hello."
  end
end

This adds a method foo to someinstance, not to its class but to that one particular instance. (Actually, foo is added to the instance's "singleton class," but that's more or less an implementation quirk.) After the above code executes, you can send method foo to someinstance:

someinstance.foo   => "Hello."

but you can't send foo to other instances of the same class. That's what << is nominally for. But people more commonly use this feature for syntactic gymnastics like this:

class Thing
  def do_something
  end

  class << self
    def foo
      puts "I am #{self}"
    end
  end
end

When this code -- this class definition -- executes, what is self? It's the class Thing. Which means class << self is the same as saying "add the following methods to class Thing." That is, foo is a class method. After the above completes, you can do this:

t = Thing.new
t.do_something  => does something
t.class.foo     => "I am Thing"
t.foo           => NoMethodError: undefined method `foo'

And when you think about what << is doing, it all makes sense. It's a way to append to a particular instance, and in the common case, the instance being appended to is a class, so the methods within the block become class methods.

In short, it's a terse way to create class methods within a class definition block. Another way would be to do this:

class Thing
  def self.foo
    # ...
  end
end

Same thing. Your example is actually a syntax error, but if you understand how << is used with instances and the class keyword, you'll know how to correct it.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
Rob Davis
  • 15,597
  • 5
  • 45
  • 49
  • 1
    Interesting! Is such method any useful in every-day programming? – Saturn Jun 02 '11 at 22:16
  • 6
    @Omega: Yea, you would see this, for instance, when you have more than one method that you want to make a class method, and you don't want to have to prefix them all with "self.". You can just put them all in that block and not worry about it. – solidcell Jan 11 '12 at 06:16
  • 1
    The last bit made it all click. Also note that `class << self` works in the sense that even though `Thing` is a class, it's also just an instance of its superclass. So there's nothing special about classes vs instances. Every class in ruby is an instance of its superclass (except for BasicObject). – ahnbizcad Nov 09 '14 at 10:27
22

<< is the syntax for "Singleton class definition". Here is an example of where/how it is "typically" used.

In a = "abc"; a << "xyz" it is the syntax for "appending data" (to string, array etc.)

Community
  • 1
  • 1
Zabba
  • 64,285
  • 47
  • 179
  • 207
  • It is used in this manner [extracted from nested_form gem](https://github.com/ryanb/nested_form/blob/master/lib/nested_form/view_helper.rb): simple_form_for(*(args << options), &block) << after_nested_form_callbacks – shadowbq Jan 28 '13 at 20:13
  • 8
    In Ruby `<<` is called the `shovel operator` – pixel 67 Nov 30 '14 at 14:50
20

If you want inheritance (based on your question title), you want a single <:

class Awesome < ParentAwesomeness

The code you provide isn't valid ruby:

class Awesomeness
end

class Awesome << Awesomeness
end

SyntaxError: (irb):3: syntax error, unexpected tLSHFT, expecting '<' or ';' or '\n'
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338