0
(max 23,23) == (max 23,23)

In above example I must put parenthesis over both methods. Is there something similar to Haskell's precedence operator: dollar($) so I can write something like this:

(max 2,3) == $ max 2,2

Or like this:

$ max 2,3 == $ max 2,3

and both examples evaluate to first example?

Or is there something that give me power over precedence in Ruby, like some keyword in method definition?

Community
  • 1
  • 1
Darek Nędza
  • 1,420
  • 1
  • 12
  • 19

1 Answers1

4

Operator precedence is not modifiable. Use parens if you want to change default precedence.

The issue is in the parser (assuming you're defining max with a splatted param); Ruby's liberal whitespace policies can create issues when it's not clear how something should be parsed.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 1
    +1. And not to mention, I think `(max 23, 23) == (max 23, 23)`, or `max(23,23) == max(23,23)` is, on sight, clearer in intent than something like `$ max 23,23 == $ max 23,23` anyway. :) – lurker Aug 13 '13 at 20:19
  • Actually, they are almost the same. The only difference is that `$` only shows one end of the part with priority, so it cannot be used for infix operators. – sawa Aug 13 '13 at 20:36
  • `max` is just method that takes 2 args @mbratch in this simple example you are right - it's 'clearer' but when you have multiple methods, so it ends like `))))` you know it will be 'ugly'. – Darek Nędza Aug 13 '13 at 20:56
  • 1
    @DarekNędza It's still the same precedence issue, though. IMO parens for precedence, particularly in deeply-nested expressions, will always win a clarity contest with isolated `$`. – Dave Newton Aug 13 '13 at 21:00