5 ms·
What's with the [] notation? Isn't there a way to "overload" the () in ruby? I.e. (sqr * inc)(5) ?
by phzbOx 15y ago
What's with the [] notation? Isn't there a way to "overload" the () in ruby? I.e.
(sqr * inc)(5) ?
- jablan 15y agoNope. Whenever you use (), the parser assumes a method call. sqr, inc and (sqr * inc) are not methods (but Proc objects), and all you can do is call _their_ methods, such as [].
- happy4crazy 15y agoNo. The (sqr * inc) part returns a lambda, and in order to call a lambda you need to either .call() it, or you can use the [] syntax.
- irahul 15y agoWelcome to Ruby - Ruby has procs, blocks, lambdas, methods which are similar yet different. Blocks aren't objects - when they are converted to objects, they become procs. Procs are like blocks and lambdas are like methods, but you can't call either of them with () syntax. And then there are multiple ways to call them: f.call() f[] f.() Take your pick.