Question

I have code like this

my_enum = [1,2].to_enum
puts my_emum.next

and it doesn't work

I understand that the enumerator is available in Ruby 1.8 as an extension. How to install it?(I'm new to ruby)

Was it helpful?

Solution

As mentioned in this answer to a different question, in Ruby 1.8.6, you can do

require 'enumerator'
6.enum_for(:times).map {...}

But I don't know if it'll allow you to do my_enum.next.

I think the documentation is at http://ruby-doc.org/stdlib/libdoc/enumerator/rdoc/ , but it seems to be down right now.

OTHER TIPS

If I fix the typo it works fine. irb session follows.

>> my_enum = [1,2].to_enum
=> #<Enumerable::Enumerator:0xb79dd700>
>> puts my_enum.next
1
>> puts my_enum.next
2

Tested in

>> VERSION
=> "1.8.7"

What version of ruby 1.8 are you running? This matters significantly.

(Also note that you have a typo in "my_emum").

In Ruby 1.8.6 , there is no "next" method for enums, just "each". Example:

my_enum = [1,2].to_enum
my_enum.each do |e|
  puts e
end

In Ruby 1.8.7, "next" is supported.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top