Difference between `->` and `lambda` in ruby [duplicate]

ساخت وبلاگ

Vote count: 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.

asked Jun 12 '12 at 16:03

1 Answer

Vote count: 1 accepted

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.

answered Jun 13 '12 at 16:17

back soft...
ما را در سایت back soft دنبال می کنید

برچسب : نویسنده : استخدام کار backsoft بازدید : 241 تاريخ : يکشنبه 10 مرداد 1395 ساعت: 18:35