Enum#each issue

I am not able to understand why I am getting the error for the method
Class: Enumerator (Ruby 1.9.3) as
below:

e = “string”.to_enum
e.each{|i| p i}

so.rb:2:in each': undefined method each’ for “string”:String
(NoMethodError)
from so.rb:2:in `’

Can anyone help me to understand what wrong I did here?

On 13 May 2013 12:16, Love U Ruby [email protected] wrote:

so.rb:2:in each': undefined method each’ for “string”:String
(NoMethodError)
from so.rb:2:in `’

String doesn’t have an #each method, as the documentation will point
out (and String doesn’t include Enumerable or anything).
Object#to_enum makes #to_enum available to all String instances, and
defaults to the method #each:

Since the Enumerator wrapper around “string” is lazy, there is no
error with “string”.to_enum, but that doesn’t mean String#each doesn’t
exist.

You might look at, for example, #bytes/#each_byte, #chars/#each_char
or #lines/#each_line (among others) to iterate over different
manifestations of the string’s data.

Adam P. wrote in post #1108763:

On 13 May 2013 12:16, Love U Ruby [email protected] wrote:

The below point I didn’t get,can you help me a bit more to understand
your sentences.

Since the Enumerator wrapper around “string” is lazy, there is no
error with “string”.to_enum, but that doesn’t mean String#each doesn’t
exist.

Here also I am getting the same errors:

When I running the below:

p e = “string”.to_enum.class #=> Enumerator
p e = “string”.to_enum #=> #<Enumerator: “string”:each>
e.next #=> so.rb:2:in <main>': undefined method next’ for
Enumerator:Class (NoMethodError)

Love U Ruby wrote in post #1108761:

I am not able to understand why I am getting the error for the method
Class: Enumerator (Ruby 1.9.3) as
below:

e = “string”.to_enum
e.each{|i| p i}

so.rb:2:in each': undefined method each’ for “string”:String
(NoMethodError)
from so.rb:2:in `’

Can anyone help me to understand what wrong I did here?

Well, as already pointed out, a string does not have an “each” method.
What are you trying to do? What result did you expect?

On 13 May 2013 12:31, Love U Ruby [email protected] wrote:

p e = “string”.to_enum.class #=> 15860404

I very much doubt this is what you got for #class. I suggest reading
Object#to_enum’s documentation, as well as what Enumerator is and does
if you’re unsure. Again, this will give no errors, even though X#each
doesn’t exist:

class X; end
X.new.to_enum

Love U Ruby wrote in post #1108765:

Adam P. wrote in post #1108763:

On 13 May 2013 12:16, Love U Ruby [email protected] wrote:

The below point I didn’t get,can you help me a bit more to understand
your sentences.

Since the Enumerator wrapper around “string” is lazy, there is no
error with “string”.to_enum, but that doesn’t mean String#each doesn’t
exist.

Here also I am getting the same errors:

When I running the below:

p e = “string”.to_enum.class #=> Enumerator
p e = “string”.to_enum #=> #<Enumerator: “string”:each>
e.next #=> so.rb:2:in <main>': undefined method next’ for
Enumerator:Class (NoMethodError)

As the first line telling e is an instance of class Enumerator, then
why the method next not working? This is my pain point.

you did something wrong again:

e = “string”.to_enum #=> #<Enumerator: “string”:each>
e.next #NoMethodError: undefined method `each’ for “string”:String

From the
Doc:Class: Enumerator (Ruby 1.9.3)

Enum#feed: Sets the value to be returned by the next yield inside e.If
the value is not set, the yield returns nil.This value is cleared after
being yielded.

I tried one example but it is not the one I think to understand the
#feed method.

a = [1,2,3,4].to_enum
p a.next #=> 1
a.feed ‘foo’
p a.next #=> 2 , I expected here ‘foo’

Can anyone give me a good example to understand how the #feed method
works?

Can anyone help me to understand the above asked question?

Thanks

On Mon, May 13, 2013 at 2:16 PM, Hans M. [email protected]
wrote:

you did something wrong again:

e = “string”.to_enum #=> #<Enumerator: “string”:each>
e.next #NoMethodError: undefined method `each’ for “string”:String


Posted via http://www.ruby-forum.com/.

to_enum will allow you to define and create a new Enumerator for the
object.
Then you can enumerate that obj by calling that method on obj.
The default is :each

As in docs:
“to_enum(method = :each, *args)”

But this implies that your object respond_to :each

… I hope this helps …

e.

IF you have READ the link you post us you would know that feed only
works on the yield INSIDE the Enumerator, not with external methods

To be fair the documentation is a bit tricky to follow there. I don’t
even know why you’d have more than one yield inside each.

On Mon, May 13, 2013 at 08:16:56PM +0900, Love U Ruby wrote:

Can anyone help me to understand what wrong I did here?

The enumerator returned is for the string “string”.

What are you trying to do?

Darryl Pierce wrote in post #1109714:

On Mon, May 13, 2013 at 08:16:56PM +0900, Love U Ruby wrote:

Can anyone help me to understand what wrong I did here?

The enumerator returned is for the string “string”.

What are you trying to do?

No I am not able to understand the method enum#feed. The doc is not
clear to me.

def meth
[1,2,3].each {|e| p yield(e)}
end

m = to_enum(:meth)
m.next #=> 1

m.feed “e”

m.next
#printed: “e”
#return => 2

as you can see, feed affects the result of yield, BUT the enumerator
method need to take care with it

WHY DONT YOU READ MY POSTS UNTIL THE END???

feed affects the result of yield, BUT the enumerator
method need to take care with it

the normal each DOES NOT!!!

there is a sample with “map!”

a = [1,2,3,4]
m=a.to_enum(:map!)

m.next #=> 1
m.feed(“a”)

m.next #=> 2

m.next #=> 3
m.feed(“b”)

m.next #=> 4

a #=> [“a”, nil, “b”, 4]

Hans M. wrote in post #1109718:

def meth
[1,2,3].each {|e| p yield(e)}
end

m = to_enum(:meth)
m.next #=> 1

Great example! but when you do m.next, how internally enumerator
works? Just need one clarification.

As I understand the below:

[1,2,4].to_enum
=> #<Enumerator: [1, 2, 4]:each>

a = [1,2,4].to_enum
=> #<Enumerator: [1, 2, 4]:each>

a.next
=> 1

But you enumerate the method meth, so when you do m.next how things
worked as output as 1? need help here.

m.feed “e”

m.next
#printed: “e”
#return => 2

as you can see, feed affects the result of yield, BUT the enumerator
method need to take care with it

Hans M. wrote in post #1109722:

Apologies to make you irritate on me. Your first example I understood.
The below one little bit hard for me.

the normal each DOES NOT!!

there is a sample with “map!”

a = [1,2,3,4]
m=a.to_enum(:map!)

m.next #=> 1
m.feed(“a”)

m.next #=> 2 Why not “a” comes out here?

m.next #=> 3
m.feed(“b”)

m.next #=> 4 why not “b” comes not here like your first example.

a #=> [“a”, nil, “b”, 4] #=> How has the output generated? from where nil
comes?

in my first example the “e” was printed with p

in this one it is not

next still returns the same values as without feed, only the function
reacts different

in my sample the nil comes because i dont call m.feed after the second
m.next

because i use the enumerator of the map! method,
feed & next does alter the Array itself

On Tue, May 21, 2013 at 11:13:25PM +0900, Love U Ruby wrote:

clear to me.
Have you tried looking at the source code for it?