0.0
No commit activity in last 3 years
No release in over 3 years
Turn your procs to lambdas in no time
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
 Project Readme

Proc To Lambda

Build Status

Convert your procs to lambdas for all the Rubies.

Why?

I love anonomyous functions ( lambdas & procs <3 ). I also love "confident" style ruby functions that have early returns:

def foo(num)
  return false if num.nil?
  # ...

Unfortunately procs do not play nice with early returns:

def bar(num, &block)
  block.call(num)
end

bar(nil) do |num|
  return false if num.nil?
  # ...
end
LocalJumpError: unexpected return
  from (irb):25:in `block in irb_binding'
  from (irb):21:in `call'
  from (irb):21:in `bar'
  from (irb):24
  from /Users/schneems/.rbenv/versions/2.0.0-p247/bin/irb:12:in `<main>'

But lambdas do:

bar(nil, &lambda {|num|
  return false if num.nil?
  # ...
})
# => false

What I want is the pretty syntax of using a proc, but with the flexibility of explicit returns of a lambda. Enter: ProcToLambda

Can't you do this in Ruby already?

Yes, but the implementation is not standard across all the rubies :(

Install

In your Gemfile:

gem 'proc_to_lambda'

Use

In your code, call ProcToLambda.to_lambda() and pass in a block:

def bar(num, &block)
  block = ProcToLambda.to_lambda(block)
  block.call(num)
end

If you're risky you can extend the Proc class directly:

Proc.extend(ProcToLambda)

Then you can call Proc#to_lambda:

def bar(num, &block)
  block.to_lambda.call(num)
end

License

MIT