Forum: Ruby How to identify whether a line of code will return?

Posted by Josh Cheek (josh-cheek)
on 2013-01-08 04:46
(Received via mailing list)
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/b...

Here is what I tried (hooking into `on_kw "return"` callback in Ripper):
https://github.com/JoshCheek/seeing_is_believing/b...
Posted by Josh Cheek (josh-cheek)
on 2013-01-08 05:10
(Received via mailing list)
On Mon, Jan 7, 2013 at 9:46 PM, Josh Cheek <josh.cheek@gmail.com> wrote:

>
> Here are some tests:
>
> 
https://github.com/JoshCheek/seeing_is_believing/b...
>
> Here is what I tried (hooking into `on_kw "return"` callback in Ripper):
>
> 
https://github.com/JoshCheek/seeing_is_believing/b...
>


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
Posted by Matthew Kerwin (mattyk)
on 2013-01-08 05:34
(Received via mailing list)
On 8 January 2013 14:09, Josh Cheek <josh.cheek@gmail.com> wrote:

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

I agree, it's pretty weird.  I would guess the "void value expression"
error is actually the parser complaining that the left hand operand of
(({&&}}) is void.  The right hand side is allowed to be void so we can 
do
things like (({foo.bad? && asplode})).  Odd that it doesn't mind the 
right
hand side of the assignment operator being void; I guess the parser 
can't
determine, and by the time it's evaluated at runtime, the (({return})) 
has
already executed, so the point is moot.

By the way, if you do manage to solve the halting problem, please let us
all know.

Cheers!
--
  Matthew Kerwin, B.Sc (CompSci) (Hons)
  http://matthew.kerwin.net.au/
  ABN: 59-013-727-651

  "You'll never find a programming language that frees
  you from the burden of clarifying your ideas." - xkcd
Posted by Robert Klemme (robert_k78)
on 2013-01-08 09:42
(Received via mailing list)
On Tue, Jan 8, 2013 at 4:46 AM, Josh Cheek <josh.cheek@gmail.com> 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:

Why do you need to modify the line?  Can't you catch it at the calling
site or by decorating the method?

Kind regards

robert
Posted by Josh Cheek (josh-cheek)
on 2013-01-08 15:35
(Received via mailing list)
On Tue, Jan 8, 2013 at 2:41 AM, Robert Klemme 
<shortcutter@googlemail.com>wrote:

> On Tue, Jan 8, 2013 at 4:46 AM, Josh Cheek <josh.cheek@gmail.com> 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:
>
> Why do you need to modify the line?  Can't you catch it at the calling
> site or by decorating the method?
>
>
Because the library I'm writing (
https://github.com/JoshCheek/seeing_is_believing) records the result of
every line. The plan is to use it in an editor so that every time the 
file
is saved, it displays the results of the lines next to them.

I don't need to record the return statement, though, that seems too
difficult to me. I just need to identify that it will blow up when 
executed
so that I don't try to record it.

-Josh
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.