Allows you to turn any method that yields, into a method that accepts no block and returns an enumerator instead.
Add this line to your application's Gemfile:
gem 'as_enum'
And then execute:
$ bundle
Or install it yourself as:
$ gem install as_enum
class Cow
as_enum def each_thing(x)
yield x
end
end
cow = Cow.new
enum = cow.each_thing(5) # => #<Enumerator: #<Cow:0x007fdb74807dc8>:each_thing(5)>
enum.next # => 5
as_enum monkey patches Module by default. If this is unwanted, require as_enum/source
instead,
and you'll be able to use as_enum as follows:
class Cow
AsEnum.as_enum self, def each_thing(x)
yield x
end
end
cow = Cow.new
enum = cow.each_thing(5) # => #<Enumerator: #<Cow:0x007fdb74807dc8>:each_thing(5)>
enum.next # => 5
- Fork it ( https://github.com/[my-github-username]/as_enum/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request