3

Pretty short question: Is it possible to use the Symbol#to_proc shorthand (e.g. lambda(&:upcase) with the stabby lambda syntax in Ruby? For example, I can say this:

p = lambda &:upcase

to get a Proc in p but I can't find an equivalent using ->. This:

p = -> &:upcase

doesn't work, of course.

1
  • 6
    My REPL told me I should come here and find Sergio to answer the question. Commented Jun 12, 2012 at 16:07

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

A minor bummer. Also, it's interesting to note that proc or lambda with no arguments return ArgumentErrors while -> returns a SyntaxError.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.