News for hackers. Stuff that matters.
Re:What's better about Ruby than Python? - comp.lang.python by mcantor on Thursday August 26, 2010 @11:06PM (#1637900)
I HATE dealing with first class functions in Ruby. I understand that it was a design choice, but man, in Python you can pass a function or a lambda and you just don't have to worry about which one you're calling. In Ruby, () will explode on a Proc object. What is with that?!!?
Re:What's better about Ruby than Python? - comp.lang.python by Twisol on Thursday August 26, 2010 @11:58PM (#1637959)
Because () doesn't call a method, it's simply used where a method call occurs. To call a callable object, you use #call or (as mentioned by epochwolf) #[], the latter of which I personally detest.

It's fun to contrast this with Lua, by the way.

Re:What's better about Ruby than Python? - comp.lang.python by mcantor on Friday August 27, 2010 @09:32AM (#1638745)
It's the inconsistency that bothers me.

In Python, you can make anything callable by providing a __call__() method, and the parentheses will work as expected. This makes functions fungible with anything, including any object or anonymous function.

In Ruby, only methods are callable, and everything else must be called with the call() method, which means you can't use one in a place where the other might be expected. They're two concepts with the same verb--call--that require different syntax. It's inconsistent.

Re:What's better about Ruby than Python? - comp.lang.python by Confusion on Friday August 27, 2010 @10:59AM (#1638993)
You can also make anything callable in Ruby: just provide a call method. The parenthesis will still work as may be expected in Ruby, but, as several folks said before: in Ruby parenthesis are not used to call anything. A method is called simply be mentioning it. Anything that follows is an argument and sometimes parameters are needed for disambiguation of the parameters. This is counterintuitive, only because it differs from most other languages. If you want to postpone evaluation, you have to wrap the method call in a closure that you .call later: in Ruby a method is not a closure and not a function pointer. It's different than in many other languages, but that alone doesn't make it inconsistent.
Re:What's better about Ruby than Python? - comp.lang.python by mcantor on Friday August 27, 2010 @11:24AM (#1639057)
Thank you--this is a good explanation of why the parentheses don't work on anonymous functions: Ruby's immediate calling essentially forces you to choose between "objects that you can call like methods" and "objects that you can easily pass around." This hearkens back to carbon8's comment (http://news.ycombinator.com/item?id=1141245); viewing Ruby's handling of first-class functions as "inconsistent" definitely betrays a Python-centric view of things.

In that sense, I see why it's not so bad; the difference between methods and anonymous functions is very explicit, and when you want to pass things around, it's likely that you will have target code which expects a Proc object and source code that generates it. Perhaps it's not so bad because there are few legitimate use cases, if any, for transporting a method when an arbitrary block is expected.

Re:What's better about Ruby than Python? - comp.lang.python by carbon8 on Friday August 27, 2010 @11:28AM (#1639067)
Procs (lambdas, procs and Proc-wrapped blocks) and Methods are callable and all respond to #call.

As wycats pointed out in a post earlier this year (http://yehudakatz.com/2010/02/21/ruby-is-not-a-callable-orie...), the reason you don't need to explicitly call #call on methods is that Ruby is designed for the common case of calling methods. In addition, it's uncommon to even explicitly use #call or #[] on Procs since calling them is built into the language via yield.

Re:What's better about Ruby than Python? - comp.lang.python by aarongough on Friday August 27, 2010 @02:07AM (#1638141)
Lua is a great little language... I only really 'discovered' it a few weeks ago and have really enjoyed what I've seen so far. I don't think it will replace Ruby for me, but I like it's minimal feel for comparison...
Re:What's better about Ruby than Python? - comp.lang.python by carbon8 on Friday August 27, 2010 @01:33AM (#1638094)
Are you actually passing around methods in Ruby and, if so, why? As I've pointing out in the past (http://news.ycombinator.com/item?id=1141245), passing around methods is very rare in Ruby due to the flexibility of Procs.
Re:What's better about Ruby than Python? - comp.lang.python by mcantor on Friday August 27, 2010 @09:37AM (#1638761)
Interesting! I am a half-breed; I write Python at my day job and Ruby in my personal projects. You seem to be suggesting that I am sort of making a mountain out of a molehill. Can you perhaps elaborate on this? I have definitely felt this pain before; I'm not just complaining about it because I "like the Python way more." But if I'm not using Ruby "as intended," I would like to know how I should be using it instead! I feel like the "Python way" is powerful because you don't have to think about whether you're dealing with an anonymous function or a function, whereas in Ruby you must know for sure which one you are dealing with, because they are invoked differently. I'm interested to know how it isn't really a problem, if that's how you feel.
Re:What's better about Ruby than Python? - comp.lang.python by carbon8 on Friday August 27, 2010 @11:32AM (#1639076)
It's not a problem in Ruby because you don't pass around method instances, you pass around Procs (wrapped blocks, lambdas and procs), so if you are passing a callable around, you know it's a Proc. If you are passing around a method instance, you should almost certainly be using a block, lambda or proc.

Also, see wycats post: http://yehudakatz.com/2010/02/21/ruby-is-not-a-callable-orie...

Re:What's better about Ruby than Python? - comp.lang.python by yxhuvud on Saturday August 28, 2010 @06:35AM (#1641570)
If you have a method that in python would take a function as a parameter then it usually will be passed as a block in ruby.

This doesn't really work if you would want to pass several procs into a method, but honestly, that is not very common.

Re:What's better about Ruby than Python? - comp.lang.python by sandal on Friday August 27, 2010 @09:18AM (#1638704)
Well, a Method object and a Proc are two different things, but somewhat similar, so it still makes sense to discuss Proc behavior here. I would like it if this worked:

    a = lambda { |x| x + 1 }
    a(3) #=> 4
But since it doesn't, the alternatives are not too bad (though likely far too numerous for Python tastes)

    a[3] #=> 4
    a.(3) #=> 4 (on Ruby 1.9)
I think that Ruby's ability to call methods without () make for better readability in the case of writing domain specific interfaces, but come at a cost in other places. Personally I think it's a worthwhile tradeoff but I can see the other side of the argument.
Re:What's better about Ruby than Python? - comp.lang.python by epochwolf on Thursday August 26, 2010 @11:25PM (#1637921)
You can call a proc with []

    p = Proc.new {|str| puts "Hello #{str}" }
    p["World"] # prints "Hello World"
:)
Re:What's better about Ruby than Python? - comp.lang.python by mcantor on Friday August 27, 2010 @09:02AM (#1638670)
I'm aware of this, but it's still not (). You still must be aware that you are dealing with an unbound proc object, and it must be handled differently than usual!