Feature #15606
closedPrecedence of -@ and +@
Description
-@ and +@ seem to have issues with method chaining:
here's another example:
I know that I can fix this by adding parentheses, i.e. (-i).negative? and (+'foo').upcase! but it feels cumbersome.
Shouldn't the above work out of the box?
Unless I'm missing a crucial use case, the precedence for -@ and +@ should be changed.
Updated by sos4nt (Stefan Schüßler) over 7 years ago
- Subject changed from Precendence of -@ and +@ to Precedence of -@ and +@
Updated by nobu (Nobuyoshi Nakada) over 7 years ago
- Status changed from Open to Feedback
Do you expect i = -1; -i.abs to return +1?
Updated by sos4nt (Stefan Schüßler) over 7 years ago
nobu (Nobuyoshi Nakada) wrote:
Do you expect
i = -1; -i.absto return+1?
Yes, indeed. I'd expect <something>.abs to return a positive value.
Likewise:
And a really weird one:
Again, I perfectly understand why this is happening and how to avoid it. I just don't think this is the way it should be, nor do I see why it could be preferable.
Updated by spinute (Satoru Horie) over 7 years ago
Compatibility is essential in Ruby. So, we need strong evidence when we break it.
I do not think the current behavior is weird.
<something>.abs returns positive value, consistently.
A programmer who knows -i.abs is evaluated as -(i.abs) can get an expected result. As a note, - in -1 is not an operator, so -1.abs == (-1).abs == 1. -i.to_s is also explained in the same way.
I agree that the behavior may be confusing for beginners.