Named Proc

When working with a lot of procs, naming them might become handy. It makes identifying them later easier, which can be utilized by your code. For example, it enables improves the usefulness of multi_block, a gem that allows you to pass multiple procs to methods.


Add to Gemfile:

gem 'named_proc'

Named Procs

A named proc acts like a normal proc, but it has a name attribute. You create it by calling a method with the desired name on proc:

>> a = proc.even?{ |e| e.even? }
=> #<NamedProc:[email protected](irb):1>
=> :even?
>> a[42]
=> false

In the same way, you can create lambdas:

>> b = lambda.doubler{ |e| e * 2 }
=> #<NamedProc:[email protected](irb):7 (lambda)>
=> :doubler
>> b[21]
=> 42
>> b.lambda?
=> true

MIT License

The original gist: