Enum#each issue

I am not able to understand why I am getting the error for the method
http://www.ruby-doc.org/core-1.9.3/Enumerator.html#method-i-each 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 `<main>'

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

···

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

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.

···

On 13 May 2013 12:16, Love U Ruby <lists@ruby-forum.com> wrote:

so.rb:2:in `each': undefined method `each' for "string":String
(NoMethodError)
  from so.rb:2:in `<main>'

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 `<main>'

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?

···

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

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

From the
Doc:http://www.ruby-doc.org/core-1.9.3/Enumerator.html#method-i-feed

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?

···

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

Can anyone help me to understand the above asked question?

Thanks

···

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

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.

···

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

The enumerator returned is for the string "string".

What are you trying to _do_?

···

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

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 `<main>'

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

--
Darryl L. Pierce <mcpierce@gmail.com>
http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"

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

···

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

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]

···

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

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

···

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

Ok! I tried the below:

2.0.0p0 :001 > a = [1,2,3,4]
=> [1, 2, 3, 4]
2.0.0p0 :002 > m=a.to_enum(:map!)
=> #<Enumerator: [1, 2, 3, 4]:map!>
2.0.0p0 :003 > m.next
=> 1
2.0.0p0 :004 > a
=> [1, 2, 3, 4]

Still ok.

2.0.0p0 :005 > m.next
=> 2
2.0.0p0 :006 > a
=> [nil, 2, 3, 4]

Now how that nil comes above? I am totally confused.

2.0.0p0 :007 > m.next
=> 3
2.0.0p0 :008 > a
=> [nil, nil, 3, 4]
2.0.0p0 :009 >

···

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

read about what map! does, and why the return of a block (or in this
case the #feed) is important

···

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

because the block is similar to a e.feed(function(e.next))

and you do not call the feed, without ruby does not know wich value it
should set for the element

···

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

its because how it works ...

first you need to call next
then you call feed to set the value,
and then the next call of next does apply the change (even if it raise
an StopIteration error

···

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

Adam Prescott wrote in post #1108763:

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 #=> 15860404
e.next #=> so.rb:2:in `<main>': undefined method `next' for
Enumerator:Class (NoMethodError)

···

On 13 May 2013 12:16, Love U Ruby <lists@ruby-forum.com> wrote:

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

···

On Mon, May 13, 2013 at 2:16 PM, Hans Mackowiak <lists@ruby-forum.com> 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/\.

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.

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

Hans Mackowiak 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

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

Hans Mackowiak 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?

···

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