Yieldhelper
I hope you enjoy yield_helper. This came about while refactoring a bunch of yielding methods of another gem I was working on.
Installation
Add this line to your application's Gemfile:
gem 'yieldhelper'
And then execute:
$ bundle
Or install it yourself as:
$ gem install yieldhelper
Usage
This works with either a method or a stand alone function. See the testbench file in the spec directory for some examples.
The one-liner looks like this:
return Enumerator.enumerate_yields(self, :my_method_name, first_parameter, second_parameter, etc) unless block_given?
The first parameter is always self
, the second parameter is a symbol of your method name.
This is followed by your parameter list if any. This one-liner is typically the first line in your method.
The one-liner will only execute if no block is given (via the unless block_given?
); in this case, the enumerate_yields
class method will
recursively call your method (named in the second parameter). The return
statement prevents fall-through when the recursion ends;
it also gives you an enumerator object instance. All you need to do is a simple copy-paste-edit on the above one-liner.
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
.
Contributing
Don't think this will ever change, so for now I will deal with it alone.
License
The gem is available as open source under the terms of the MIT License.