Forum: Ruby Eval Weird Behavior

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ricardo S. (Guest)
on 2009-03-19 07:45
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.
Ricardo S. (Guest)
on 2009-03-19 08:50
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
This topic is locked and can not be replied to.