From: Bill Kelly Date: 2002-12-11T08:21:43+09:00 Subject: Re: Seeking Ruby Samadhi Hi, From: "Jake" > class Array > def each2 > each {|value| puts value} > end > end > [1, 2, 3].each2 > > > **HOW IN THE NAME OF ALAN TURING DOES _each2_ PASS _value_ TO > _each_***?!?! :) It's like: class Array def each2 self.each {|value| puts value} end end where each is talking to an implicit "self". Other variations: class Array def each3 0.upto(length - 1) {|value| puts value} end def each4 (0...length).each {|value| puts value} end end In the above, length is self.length implicitly. The values are coming from iterators based on values obtained from "self", rather than passed in from each2, each3, or each4... Another example, replacing "each" itself with our own version: irb> class Array irb> def each irb> 0.upto(length - 1) {|idx| yield self.at(idx) } # could also use each_index {} irb> self irb> end irb> def each2 irb> each {|value| puts value} irb> end irb> end nil irb> [1,2,3].each2 1 2 3 ...in case it's useful to see how "each" itself might be implemented... Hope this helps, Bill