G
Glenn Ritz
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}" }
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, as in the following example:
%w(cat dog mouse bird).each.with_index { |e, i| puts "i: #{i}, e: #{e}"
}
I realize that I could have just called each_with_index on the array
(instead of 'each.with_index') and it probably would have been more
straightforward, but the above code is just for example purposes.
I believe that this would involve opening up the Enumerable module and
defining a with_self method, but I do not know what that method should
look like.
Any help would be appreciated.
Thanks!
Glenn
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, as in the following example:
%w(cat dog mouse bird).each.with_index { |e, i| puts "i: #{i}, e: #{e}"
}
I realize that I could have just called each_with_index on the array
(instead of 'each.with_index') and it probably would have been more
straightforward, but the above code is just for example purposes.
I believe that this would involve opening up the Enumerable module and
defining a with_self method, but I do not know what that method should
look like.
Any help would be appreciated.
Thanks!
Glenn