Inside it will be implemented something like this:
module Enumerable
def each_with_index
i = 0
each do |elem|
yield elem, i
i += 1
end
end
end
But you could consider it like this:
class Array
def each_with_index
size.times do |i|
yield self[i], i
end
end
end
Hi Brian,
Just some follow-up questions for these codes:
I recall the the method call in Ruby follow this format:
"receiver.method"
So
1) which one is the reciever for "each"?
2) which one is the reciever for "size"?
When there's no explicit receiver, the method call happens on the
object that is "self" at that point.
Jesus.
···
On Sun, Apr 11, 2010 at 5:32 PM, Li Chen <chen_li3@yahoo.com> wrote:
Brian Candler wrote:
Inside it will be implemented something like this:
module Enumerable
def each_with_index
i = 0
each do |elem|
yield elem, i
i += 1
end
end
end
But you could consider it like this:
class Array
def each_with_index
size.times do |i|
yield self[i], i
end
end
end
Hi Brian,
Just some follow-up questions for these codes:
I recall the the method call in Ruby follow this format:
"receiver.method"
So
1) which one is the reciever for "each"?
2) which one is the reciever for "size"?