Forum: Ruby precedence of single-line rescue and assignment

Fbb4d027695dfdf76bf448b15d7e306a?d=identicon&s=25 matt neuburg (Guest)
on 2008-05-07 19:40
(Received via mailing list)
def boom
  raise "boom"
end
y = ""
y = boom rescue "fine"
puts y # "fine"

So far so good; the precedence of "rescue" is higher than that of
assignment. But now:

def boom
  raise "boom"
end
y = ""
y += boom rescue "fine"
puts y # ""

How does the precedence work here, and should this behavior be
considered a possible bug? m.
Fbb4d027695dfdf76bf448b15d7e306a?d=identicon&s=25 matt neuburg (Guest)
on 2008-05-07 20:56
(Received via mailing list)
matt neuburg <matt@tidbits.com> wrote:

> def boom
>   raise "boom"
> end
> y = ""
> y += boom rescue "fine"
> puts y # ""

I think what's really going on here is that what we often see described
as a single-line use of "rescue" is actually a single-*expression* use
of "rescue", since the above can be fixed using parentheses:

y += (boom rescue "fine")

I hadn't realized this was even legal. m.
699c00ad35f2755810b4aa5f423d73e2?d=identicon&s=25 Albert Schlef (alby)
on 2008-05-07 21:27
matt neuburg wrote:
> [...] since the above can be fixed using parentheses:
>
> y += (boom rescue "fine")

But if `y += boom rescue "fine"` indeed translates into `y += (boom
rescue "fine")` that still doesn't solve the mystery. We'd end up with
`y += ("fine")`. We'd get `y == "fine"', which isn't the case...

That's strange!
Fe5660da5930df2d7b34b6066c2d16bb?d=identicon&s=25 Jens Wille (Guest)
on 2008-05-07 21:52
(Received via mailing list)
Albert Schlef [2008-05-07 21:27]:
> matt neuburg wrote:
>> [...] since the above can be fixed using parentheses:
>>
>> y += (boom rescue "fine")
>
> But if `y += boom rescue "fine"` indeed translates into `y +=
> (boom rescue "fine")` that still doesn't solve the mystery.
no, it evaluates to '(y += boom) rescue "fine"'. the '+=' binds
tighter than/takes precedence over 'rescue'.

  def boom; p 'boom'; raise 'boom'; end
=>nil
  def fine; p 'fine'; 'fine'; end
=>nil
  y = ''
=>""
  y += boom rescue fine
"boom"
"fine"
=>"fine"
  y
=>""
  (y += boom) rescue fine
"boom"
"fine"
=>"fine"
  y
=>""
  y += (boom rescue fine)
"boom"
"fine"
=>"fine"
  y
=>"fine"

cheers
jens
Fbb4d027695dfdf76bf448b15d7e306a?d=identicon&s=25 matt neuburg (Guest)
on 2008-05-07 22:16
(Received via mailing list)
Jens Wille <jens.wille@uni-koeln.de> wrote:

> Albert Schlef [2008-05-07 21:27]:
> > matt neuburg wrote:
> >> [...] since the above can be fixed using parentheses:
> >>
> >> y += (boom rescue "fine")
> >
> > But if `y += boom rescue "fine"` indeed translates into `y +=
> > (boom rescue "fine")` that still doesn't solve the mystery.
> no, it evaluates to '(y += boom) rescue "fine"'. the '+=' binds
> tighter than/takes precedence over 'rescue'.

Right, that's what I said. The mystery is why

y += boom rescue "fine"

translates to

(y += boom) rescue "fine"

but

y = boom rescue "fine"

translates to

y = (boom rescue "fine")

What I'm saying is: Doesn't the user reasonably expect that = and +=
will have similar precedence? I don't really care about this any more,
now that I know that I can disambiguate for myself using parentheses;
but it feels like a bug, somehow. m.
Fe5660da5930df2d7b34b6066c2d16bb?d=identicon&s=25 Jens Wille (Guest)
on 2008-05-07 23:34
(Received via mailing list)
matt neuburg [2008-05-07 22:15]:
> What I'm saying is: Doesn't the user reasonably expect that = and
> += will have similar precedence?
well, maybe yes. the pickaxe even lists them all as having the same
precedence [1]. but as you can easily verify, all operator
assignments in fact have a higher precedence than plain assignment.

maybe it's because "lhs '=' arg kRESCUE_MOD arg" is a special case
in parse.y [2] over the more generic "stmt kRESCUE_MOD stmt"? i
don't know. i'm far from claiming that i have grokked ruby's parsing ;-)

however, whether this is to be considered a bug, i'm not in the
position to judge...

[1]
<http://phrogz.net/ProgrammingRuby/language.html#op...
[2] <http://svn.ruby-lang.org/repos/ruby/branches/ruby_...

cheers
jens
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2008-05-08 01:55
(Received via mailing list)
On Wed, May 7, 2008 at 10:15 PM, matt neuburg <matt@tidbits.com> wrote:
>  > no, it evaluates to '(y += boom) rescue "fine"'. the '+=' binds
>  but
>  but it feels like a bug, somehow. m.
And worse

y += boom rescue fine

is not equivalent to

y = y + boom rescue fine

that is not what I understand under syntactic sugar, but the behavior
is all the same in MRI1.8 YARV and Jruby, that is not a nice thing.
Well spotted indeed.

Cheers
Robert
>
>
>
>  --
>  matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/
>  Leopard - http://www.takecontrolbooks.com/leopard-customizing.html
>  AppleScript - http://www.amazon.com/gp/product/0596102119
>  Read TidBITS! It's free and smart. http://www.tidbits.com
>
>



--
http://ruby-smalltalk.blogspot.com/

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein
Fbb4d027695dfdf76bf448b15d7e306a?d=identicon&s=25 matt neuburg (Guest)
on 2008-05-08 03:11
(Received via mailing list)
Robert Dober <robert.dober@gmail.com> wrote:

> On Wed, May 7, 2008 at 10:15 PM, matt neuburg <matt@tidbits.com> wrote:

>
> y = y + boom rescue fine
>
> that is not what I understand under syntactic sugar, but the behavior
> is all the same in MRI1.8 YARV and Jruby, that is not a nice thing.
> Well spotted indeed.

If we think this might be a bug, how do I report it? m.
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2008-05-08 04:08
(Received via mailing list)
Hi,

In message "Re: precedence of single-line rescue and assignment"
    on Thu, 8 May 2008 10:10:07 +0900, matt@tidbits.com (matt neuburg)
writes:

|> y += boom rescue fine
|>
|> is not equivalent to
|>
|> y = y + boom rescue fine
|>
|> that is not what I understand under syntactic sugar, but the behavior
|> is all the same in MRI1.8 YARV and Jruby, that is not a nice thing.
|> Well spotted indeed.
|
|If we think this might be a bug, how do I report it? m.

I recognized the problem.

              matz.
Fbb4d027695dfdf76bf448b15d7e306a?d=identicon&s=25 matt neuburg (Guest)
on 2008-05-08 06:40
(Received via mailing list)
Yukihiro Matsumoto <matz@ruby-lang.org> wrote:

> |>
> |> that is not what I understand under syntactic sugar, but the behavior
> |> is all the same in MRI1.8 YARV and Jruby, that is not a nice thing.
> |> Well spotted indeed.
> |
> |If we think this might be a bug, how do I report it? m.
>
> I recognized the problem.

Wonderful, thanks! m.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.