From: Aaron Patterson Date: 2010-02-19T08:03:04+09:00 Subject: Re: Adding self to Enumerable On Fri, Feb 19, 2010 at 06:39:49AM +0900, Glenn Ritz wrote: > Ryan Davis wrote: > > On Feb 18, 2010, at 11:57 , Glenn Ritz wrote: > > > >> Hi, > >> > >> I would like to add self to an Enumerable object in Ruby 1.9, like in > >> this (admittedly) contrived example: > >> > >> %w(cat dog mouse).each.with_self { |e, s| puts "self: #{s.inspect}, e: #{e}" } > > > > IDGI. What's the point? > > > > %w(cat dog mouse).each { |e| s = self; puts "self: #{s.inspect}, e: > > #{e}" } > > > > or better: > > > > %w(cat dog mouse).each { |e| puts "self: #{self.inspect}, e: #{e}" } > > > I want to be able to refer to the array receiver inside of the block. > In this code: > > %w(cat dog mouse).each { |e| s = self; puts "self: #{s.inspect}, e: > #{e}" } > > self is main (or whatever class you execute the code in), not the array. OH! I have an *awesome* solution: %w(froot loops).instance_eval { each { |e| puts "self: %#{self.inspect}, e: #{e}" } } -- Aaron Patterson http://tenderlovemaking.com/