Eval Weird Behavior

R

Ricardo Sisnett

Hi all... I'm doing some Genetic Programming for my AI class, and I ran
into a weird "bug" or behavior with the "eval()" function.

Mainly what I do is build a three of evaluable nodes, and then pour the
structure into a string, which I escape in order to make it "evaluable".

So after a run, a possible program would look like:

"whileInCorridorRange(\"do2(\\\"turnRight()\\\",\\\"turnRight()\\\")\")"

- whileInCorridorRange will check for a ceratain condition, and while
it is true it will call eval for the rest of the string "do2..." same
would be for do2 which would call turnRight() and then turnRight()..
turnRight() itself is a terminal so it won't call anyone else.

This works pretty good and causes no problem... but if I go deeper into
nesting:
"whileInCorridorRange(\"ifConvexCorner(\\\"turnRight()\\\",
\\\"do2(\\\\\"whileTooFarFromWall(\\\\\\\"turnRight()\\\\\\\")\\\\\",\\\\\"whileInCorridorRange(\\\\\\\"turnLeft()\\\\\\\")\\\\\")\\\")\")"

I will get:

SyntaxError: (eval):1:in `do': compile error
(eval):1: syntax error, unexpected tIDENTIFIER, expecting ')'
whileInCorridorRange("ifConvexCorner(\"turnRight()\",
\"do2(\\"whileTooFarFromWall(\\\"turnRight()\\\")\\",\\"whileInCorridorRange(\\\"turnLeft()\\\")\\")\")")
^
(eval):1: syntax error, unexpected ',', expecting $end
whileInCorridorRange("ifConvexCorner(\"turnRight()\",
\"do2(\\"whileTooFarFromWall(\\\"turnRight()\\\")\\",\\"whileInCorridorRange(\\\"turnLeft()\\\")\\")\")")
^
from ./robot.rb:107:in `do'
from ./geneticProgram.rb:128:in `eval'
from ./robot.rb:107:in `do'
from ./geneticProgram.rb:128:in `evaluate'
from (irb):34


It looks like, using five slashes \\\\\" to escape the quotes cause the
eval function to break.

I'm not sure if this is a bug or has some "meant to be" explanation, but
I'd really appreciate any pointers.

Thanks.
 
R

Ricardo Sisnett

Ok I'm answering my own question, just in case anyone stumbles across
this issue and ends in my post:

I thought you had to add a pair of slashes (\\) to escape the quotes(")
but it seems that you have to add twice the last number of slashes plus
one...

So if you had something like this:

class Test
def a(arg)
eval(arg)
end
def b
puts "b"
end
end

In order to recursively call function a and pass "b" to the las call in
order to get a printed b... you'd have to:

1 Call:
eval("a(\"b\")")
=>b
--One Slash
2 Calls:
eval("a(\"a(\\\"b\\\")\")")
--Three Slashes ( 1 * 2 + 1)

3 Calls:
eval("a(\"a(\\\"a(\\\\\\\"b\\\\\\\")\\\")\")")
--Seven Slashes ( 3 * 2 + 1)

4 Calls:
--Fifteen Slashes (7*2 +1 )



I'm not sure if this is useful for anyone besides me and my pretty
small-scoped project, but who would know?


Thanks again
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top