From: Li Chen on
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"?

Thanks,

Li

--
Posted via http://www.ruby-forum.com/.

From: Jesús Gabriel y Galán on
On Sun, Apr 11, 2010 at 5:32 PM, Li Chen <chen_li3(a)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"?

When there's no explicit receiver, the method call happens on the
object that is "self" at that point.

Jesus.

From: Li Chen on
Jesús Gabriel y Galán wrote:
> On Sun, Apr 11, 2010 at 5:32 PM, Li Chen <chen_li3(a)yahoo.com> wrote:
>>> � � end
>>> � end
>> 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.

Hi Gabriel,

Where can I find more info about method call on implicit receiver?

Li

--
Posted via http://www.ruby-forum.com/.

From: Caleb Clausen on
On 4/11/10, Li Chen <chen_li3(a)yahoo.com> wrote:
> Where can I find more info about method call on implicit receiver?

Seems like we've had a number of newbies who've been perplexed about
this recently. Maybe this should be in the faq. Isn't there a faq
somewhere?

First  |  Prev  | 
Pages: 1 2
Prev: win32ole question
Next: Hpricot inner_html