Adding self to Enumerable

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
 
G

Glenn Ritz

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
 
R

Ryan Davis

Hi,
=20
I would like to add self to an Enumerable object in Ruby 1.9, like in
this (admittedly) contrived example:
=20
%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 =3D self; puts "self: #{s.inspect}, e: =
#{e}" }

or better:

%w(cat dog mouse).each { |e| puts "self: #{self.inspect}, e: #{e}" }
 
R

Ryan Davis

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

We on (e-mail address removed) don't care. Please refrain from needless =
repetition. thanks.
 
G

Glenn Ritz

Ryan said:
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.
 
S

Sebastian Hungerecker

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
 
A

Aaron Patterson

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}"
}
}
 
G

Glenn Ritz

Posted by Aaron Patterson (Guest)
OH! I have an *awesome* solution:
%w(froot loops).instance_eval {
each { |e|
puts "self: %#{self.inspect}, e: #{e}"
}
}


Sebastian said:
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.
 
R

Robert Klemme

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
 
G

Glenn Ritz

Robert said:
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. :)

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
 
R

Robert Klemme

2010/2/19 Glenn Ritz said:
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. =A0So I was thinking that it would be good if I could refer to th= e
array itself within the block. =A0I realize now that there are probably
better ways to do this. :)

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]
=3D> 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]
=3D> 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. =A0So 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

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top