Adding self to Enumerable

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

Hi,

I posted this earlier but did not enable email notification. I’ve done
so now. Apologies for the repetition.

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

On Feb 18, 2010, at 11:57 , Glenn R. 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}” }

Ryan D. wrote:

On Feb 18, 2010, at 11:57 , Glenn R. 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.

On Feb 18, 2010, at 12:43 , Glenn R. wrote:

I posted this earlier but did not enable email notification. I’ve done
so now. Apologies for the repetition.

We on [email protected] don’t care. Please refrain from needless
repetition. thanks.

On 18.02.2010 20:57, Glenn R. wrote:

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

Posted by Aaron P. (Guest)

OH! I have an awesome solution:

%w(froot loops).instance_eval {
each { |e|
puts “self: %#{self.inspect}, e: #{e}”
}
}

Sebastian H. wrote:

module Enumerable
def with_self
return enum_for(:with_self) unless block_given?
each {|e| yield e, self}
end
end

Thanks for the great solutions.

On Fri, Feb 19, 2010 at 06:39:49AM +0900, Glenn R. wrote:

IDGI. What’s the point?
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}”
}
}

Robert K. wrote:

On 02/18/2010 10:39 PM, Glenn R. wrote:

%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.

What is the point of that? I mean, you can always have a reference to
the instance around.

s = %w(cat dog mouse)
s.each {|e| …}

Or, in 1.9

%w(cat dog mouse).tap {|s| s.each {|e| …}}

Even if you pass around a block for later usage with #each you can make
sure the collection is accessible inside the block.

What is the real use case of this? Passing the collection into the
block during iteration feels wrong. For example, typically when
iterating modifications of the underlying collection are dangerous. Why
do you need this feature?

Kind regards

robert

Sometimes in statistics it’s good to be able to access the previous
element in a collection, not just the current one like in Ruby’s each
method. So I was thinking that it would be good if I could refer to the
array itself within the block. I realize now that there are probably
better ways to do this. :slight_smile:

Also, I find the with_index method on Enumerable objects interesting and
was trying to figure out a way to write a similar method. So I asked
the question just to learn more on what I think is an interesting
feature of the language.

Thanks,

Glenn

2010/2/19 Glenn R. [email protected]:

%w(cat dog mouse).tap {|s| s.each {|e| …}}
element in a collection, not just the current one like in Ruby’s each
method. So I was thinking that it would be good if I could refer to the
array itself within the block. I realize now that there are probably
better ways to do this. :slight_smile:

Enumerable#each_cons comes to mind…

Also, Enumerable#inject works pretty well:

irb(main):003:0> (1…5).inject {|a,b| p [a,b];b}
[1, 2]
[2, 3]
[3, 4]
[4, 5]
=> 5
irb(main):004:0> (1…5).inject(nil) {|a,b| p [a,b];b}
[nil, 1]
[1, 2]
[2, 3]
[3, 4]
[4, 5]
=> 5
irb(main):005:0>

Also, I find the with_index method on Enumerable objects interesting and
was trying to figure out a way to write a similar method. So I asked
the question just to learn more on what I think is an interesting
feature of the language.

I hope you found out.

Kind regards

robert

On 02/18/2010 10:39 PM, Glenn R. wrote:

%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.

What is the point of that? I mean, you can always have a reference to
the instance around.

s = %w(cat dog mouse)
s.each {|e| …}

Or, in 1.9

%w(cat dog mouse).tap {|s| s.each {|e| …}}

Even if you pass around a block for later usage with #each you can make
sure the collection is accessible inside the block.

What is the real use case of this? Passing the collection into the
block during iteration feels wrong. For example, typically when
iterating modifications of the underlying collection are dangerous. Why
do you need this feature?

Kind regards

robert