4

Possible Duplicate:
Interested in what the “<<” does

I read Ruby class inheritance: What is `<<` (double less than)?. I understood it helps to create methods for instances (kind of inheritance). But I came across a code:

threads << Thread.new(page) { |myPage|
h = Net::HTTP.new(myPage, 80)
puts "Fetching: #{myPage}"
resp, data = h.get('/', nil )
puts "Got #{myPage}:  #{resp.message}"
}

where threads is an array. Could somebody explain the usage of << with objects instead of classes?

Community
  • 1
  • 1

1 Answers1

11

The << operator can be overloaded to do essentially anything, because it's just a method. A class is free to define its own behaviour for the << operator. In this case, threads is an array or array-like object, and typical array semantics use << as an alias for push. The code is simply appending a new Thread onto an array called threads.

user229044
  • 232,980
  • 40
  • 330
  • 338