# Eval Weird Behavior

**URL:** <https://www.ruby-forum.com/t/eval-weird-behavior/163314>\
**Category:** Ruby\
**Created:** [March 19, 2009, 6:45am UTC](https://www.ruby-forum.com/t/eval-weird-behavior/163314 "2009-03-19T06:45:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsisnett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bcef8e/32.png) [@rsisnett](https://www.ruby-forum.com/u/rsisnett)\
**Post date:** [March 19, 2009, 6:45am UTC](https://www.ruby-forum.com/t/eval-weird-behavior/163314/1 "2009-03-19T06:45:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rsisnett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bcef8e/32.png) [@rsisnett](https://www.ruby-forum.com/u/rsisnett)\
**Post date:** [March 19, 2009, 7:50am UTC](https://www.ruby-forum.com/t/eval-weird-behavior/163314/2 "2009-03-19T07:50:35Z")

</div>

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
