Symbol#To_Proc Shorthand with the Stabby Lambda Syntax

Symbol#to_proc shorthand with the stabby lambda syntax

Apparently, it is not supported.

I think it has something to do with the fact that proc and lambda are actually methods, and not keywords. They support the same features that we usually associate with each and the other methods from the Enumerable module. However, -> is a special language construct which is parsed separately.

I can't think of a reason why something like -> &:method shouldn't be possible, but as of now the syntax of the Ruby language simply doesn't allow it.

Using Symbol#to_proc with multiple procs

If you are looking for a good architectural solution then you need to implement FIFO stack for that.

Or you can do it like you mentioned above:

Object.tap(&:do_work).tap(&:do_more_work)

What do you call the - operator in Ruby?

In Ruby Programming Language ("Methods, Procs, Lambdas, and Closures"), a lambda defined using -> is called lambda literal.

succ = ->(x){ x+1 }
succ.call(2)

The code is equivalent to the following one.

succ = lambda { |x| x + 1 }
succ.call(2)

Informally, I have heard it being called stabby lambda or stabby literal.

What is the - (stab) operator in Ruby?

It's the Ruby 1.9 "stabby lambda" operator. For example, see this article from 2008.

Nutshell:

> foo2 = ->(arg) { arg * 2 }
> foo2.call "now"
=> nownow

Note the lack of space between -> and (arg), that's intentional.

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.

Explicitly yielding n values to Symbol#to_proc

Here's how you can do it:

class Enumerator
def explicitly
each { |e| yield(*e) }
end
end

I executed your tests against this and the proper results are returned.

UPDATE: Changed to not capture the block explicitly.



Related Topics



Leave a reply



Submit