On 18.02.2010 20:57, Glenn Ritz wrote:
> 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}" }
>
> The results of the above code would be:
>
> self: ['cat','dog', 'mouse'], e: cat
> self: ['cat','dog', 'mouse'], e: dog
> self: ['cat','dog', 'mouse'], e: mouse
>
> I think of the 'with_self' method (which I want to define) as being
> similar to the with_index method [...]
>    
module Enumerable
   def with_self
     return enum_for(:with_self) unless block_given?
     each {|e| yield e, self}
   end
end