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?
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With