Prev: win32ole question
Next: Hpricot inner_html
From: Li Chen on 11 Apr 2010 11:32 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 11 Apr 2010 16:10 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 11 Apr 2010 18:34 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 11 Apr 2010 23:21
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? |