From: Josh Cheek Date: 2013-01-08T13:09:38+09:00 Subject: Re: How to identify whether a line of code will return? --f46d042f93500aad6804d2bf1d8c Content-Type: text/plain; charset=ISO-8859-1 On Mon, Jan 7, 2013 at 9:46 PM, Josh Cheek wrote: > Hi, I want to be able to identify whether a line of code will return, > because I want to modify it to capture it's return value. If it returns, > this results in a syntax error: > > $ echo 'def m() a = return end' | ruby -c > -:1: void value expression > > $ echo $? > 1 > > > Here are some tests: > > https://github.com/JoshCheek/seeing_is_believing/blob/54f4044e1c858777f8b0beb690b9dafa9d497106/spec/syntax_analyzer_spec.rb#L114-126 > > Here is what I tried (hooking into `on_kw "return"` callback in Ripper): > > https://github.com/JoshCheek/seeing_is_believing/blob/54f4044e1c858777f8b0beb690b9dafa9d497106/lib/seeing_is_believing/syntax_analyzer.rb#L123-138 > Thought maybe I could solve it with regexes, but I've come to the conclusion that I don't understand the return keyword at all :/ I can't explain cases like this, for example: $ echo '-> { a = (return && 1) }' | ruby -c -:1: void value expression -> { a = (return && 1) } ^ $ echo '-> { a = (1 && return) }' | ruby -c Syntax OK --f46d042f93500aad6804d2bf1d8c Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Mon, Jan 7, 2013 at 9:46 PM, Josh Cheek <josh.cheek@gmail.com>= ; wrote:
Hi, I want to be able to identify w= hether a line of code will return, because I want to modify it to capture i= t's return value. If it returns, this results in a syntax error:


Thought maybe I coul= d solve it with regexes, but I've come to the conclusion that I don'= ;t understand the return keyword at all :/

I can&#= 39;t explain cases like this, for example:

$ echo '-> { a =3D (return && = 1) }' | ruby -c
-:1: void value expression
-> { = a =3D (return && 1) }
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 ^

$ echo '-> { a =3D (1 && return) }' | ruby -c=
Syntax OK
--f46d042f93500aad6804d2bf1d8c--