From: dblack@... Date: 2006-09-26T06:24:38+09:00 Subject: Re: Riddle me this (a question about expressions) Hi -- On Tue, 26 Sep 2006, Rick DeNatale wrote: > On 9/25/06, dblack@wobblini.net wrote: >> Hi -- >> >> On Tue, 26 Sep 2006, Rick DeNatale wrote: >> >> > On 9/25/06, dblack@wobblini.net wrote: >> >> Hi -- >> >> >> >> On Mon, 25 Sep 2006, Rick DeNatale wrote: >> >> >> >> > On 9/24/06, dblack@wobblini.net wrote: >> >> >> >> >> >> I am fairly certain that the return value can never be captured. At >> >> >> least I can't figure out how to do it. >> >> >> >> >> >> In this: >> >> >> >> >> >> def raise_value >> >> >> x = raise >> >> >> rescue >> >> >> x >> >> >> end >> >> >> >> >> >> p raise_value # nil >> >> >> >> >> >> I'm pretty sure that x is nil because of the thing where the parser >> >> >> sees an assignment and defines the variable. I don't think an >> >> >> assignment ever actually takes place -- as witness the fact that: >> >> >> >> >> >> x = raise || 1 >> >> >> >> >> >> also leaves x as nil. >> >> > >> >> > But try: >> >> > >> >> > x = raise rescue nil || 1 >> >> > >> >> > Of course this is a trivial example, but the point is that raise can >> >> > in fact have a value. >> >> >> >> Even more trivial: >> >> >> >> x = raise rescue 1 >> >> >> >> :-) But I'd still say that the return value of the call to raise is >> >> not what's being captured in x. >> > >> > Well, from a syntax point of view, it sure LOOKs like the rvalue of >> > the assignment is the expression "raise rescue 1" >> >> It is; that expression evaluates to 1, and that's what x gets. But I >> don't think the call to raise is, itself, returning 1. >> > Regardless of what happens under the covers with setjmp/longjmp, I > can't see anyway to explain the results syntactically other than > saying that the 'raise .. rescue ...' is an rvalue in the assigment. I still agree with you :-) I find it interesting to look at what raise is actually doing/returning, but clearly x gets its 1 from that whole expression, and there's no way (that I can see) to drive a wedge between the calling of raise and the execution of the rescue clause. > The genesis of this thread was why the ruby compiler disallows: > > x = return y > > but allows > > x = raise rescue 1 > > And some have expressed the opinion that both should be disallowed. > > I can't agree. Neither can I. The rescue modifier is a special dispensation, but raise is still just a method call and shouldn't cause a syntax error. David -- David A. Black | dblack@wobblini.net Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org