Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby class inheritance: What is `<<` (double less than)?

Tags:

ruby

class << Awesomeness 

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

like image 674
Voldemort Avatar asked May 31 '11 03:05

Voldemort


People also ask

What does >> mean in Ruby?

In ruby '<<' operator is basically used for: Appending a value in the array (at last position) [2, 4, 6] << 8 It will give [2, 4, 6, 8]

What is inheritance in Ruby on Rails?

Inheritance is when a class receives or inherits the attributes and behavior of another class. The class that is inheriting the behavior is called the subclass (or derived class) and the class it inherits from is called the superclass (or base class). Imagine several classes - Cat, Dog, Rabbit, and so on.


2 Answers

<< 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.)

like image 45
Zabba Avatar answered Oct 06 '22 01:10

Zabba


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.

like image 57
Rob Davis Avatar answered Oct 06 '22 00:10

Rob Davis