How to identify whether a line of code will return?

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:

Here is what I tried (hooking into on_kw "return" callback in Ripper):

On Mon, Jan 7, 2013 at 9:46 PM, Josh C. [email protected] wrote:

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 :confused:

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

On 8 January 2013 14:09, Josh C. [email protected] 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 K., 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

On Tue, Jan 8, 2013 at 2:41 AM, Robert K.
[email protected]wrote:

On Tue, Jan 8, 2013 at 4:46 AM, Josh C. [email protected] 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

On Tue, Jan 8, 2013 at 4:46 AM, Josh C. [email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs