Ruby: Convert Proc to Lambda

How to convert method or lambda to non-lambda proc

There is no way to do this.

Besides the argument passing, I wonder what you would expect from a return in the method. It can only behave in lambda way...

If you really have to do this, you will need to build your own block, e.g.

Proc.new{ a }

For a more generic way, you'll have to check the arity of the method and pass only the required parameters.

How do I create a reusable block/proc/lambda in Ruby?

Create a lambda and then convert to a block with the & operator:

isodd = lambda { |i| i % 2 == 1 }
[1,2,3,4].select(&isodd)

Create a ruby Proc from a string

This works

eval  "lambda { " + code_string + " }"

I just don't know why this one does and the other does not.

What does it mean by prefixing a & to a raw proc object (not lambda) in Ruby?


What does & do here?

Exactly that, convert proc to a block.

I don't have a block. What I have is a raw proc object (not lambda)

Do you perhaps think these (proc and lambda) are two completely different entities? They're not. They're almost the same thing.

proc {} # => #<Proc:0x00007fe50882ecc8@-:1>
-> {} # => #<Proc:0x00007fe50882e840@-:2 (lambda)>

Certainly the same thing as far as & operator is concerned.

Idiomatic way to convert class method to proc in ruby

Would method be what you're looking for? It can let you save a method to a variable.

2.1.0 :003 > m = Kernel.method(:puts)
=> #<Method: Kernel.puts>
2.1.0 :004 > m.call('hi')
hi

Passing a lambda as a block

Tack an ampersand (&) onto the argument, for example:

("A".."K").each &procedure

This signifies that you're passing it as the special block parameter of the method. Otherwise it's interpreted as a normal argument.

It also mirrors they way you'd capture and access the block parameter inside the method itself:

# the & here signifies that the special block parameter should be captured
# into the variable `procedure`
def some_func(foo, bar, &procedure)
procedure.call(foo, bar)
end

some_func(2, 3) {|a, b| a * b }
=> 6

Convert bloc to lambda in JRuby

As long as you're running jruby 1.9-compatibly (e.g. jruby --1.9 -S irb), it should be the same:

my_proc = proc { |x| x }
my_lambda = lambda &my_proc
my_lambda.lambda?
# => true

When to use lambda, when to use Proc.new?

Another important but subtle difference between procs created with lambda and procs created with Proc.new is how they handle the return statement:

  • In a lambda-created proc, the return statement returns only from the proc itself
  • In a Proc.new-created proc, the return statement is a little more surprising: it returns control not just from the proc, but also from the method enclosing the proc!

Here's lambda-created proc's return in action. It behaves in a way that you probably expect:

def whowouldwin

mylambda = lambda {return "Freddy"}
mylambda.call

# mylambda gets called and returns "Freddy", and execution
# continues on the next line

return "Jason"

end


whowouldwin
#=> "Jason"

Now here's a Proc.new-created proc's return doing the same thing. You're about to see one of those cases where Ruby breaks the much-vaunted Principle of Least Surprise:

def whowouldwin2

myproc = Proc.new {return "Freddy"}
myproc.call

# myproc gets called and returns "Freddy",
# but also returns control from whowhouldwin2!
# The line below *never* gets executed.

return "Jason"

end


whowouldwin2
#=> "Freddy"

Thanks to this surprising behavior (as well as less typing), I tend to favor using lambda over Proc.new when making procs.



Related Topics



Leave a reply



Submit