I just wondered whether there is any good reason for or even an advantage in having to invoke Procs using proc.call(args) in Ruby, which makes higher-order function syntax much more verbose and less intuitive.
Why not just proc(args)? Why draw a distinction between functions, lambdas and blocks? Basically, it’s all the same thing so why this confusing syntax? Or is there any point for it I don’t realize?
You need some way to distinguish between calling the
Procand passing it around.In Python and ECMAScript, it’s simple: with parentheses it’s a call, without it’s not. In Ruby, leaving off the parentheses is also a call, therefore, there must be some other way to distinguish.
In Ruby 1.8,
Proc#calland its aliasProc#[]serve that distinction. As of Ruby 1.9,obj.(arg)is syntactic sugar forobj.call(arg)andProc#()is also an alias forProc#call.So, you can call a
Proclike this:And you can even also define
()for your own classes.BTW: the same problem is also why you have to use the
methodmethod to get a hold of a method object.