Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

double less than ruby [duplicate]

Tags:

ruby

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?

like image 915
Kalyana Sundaram Avatar asked Dec 27 '22 12:12

Kalyana Sundaram


1 Answers

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.

like image 137
meagar Avatar answered Dec 29 '22 01:12

meagar