From: Ricardo Sisnett Date: 2009-03-19T14:43:20+09:00 Subject: Eval Weird Behavior 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. -- Posted via http://www.ruby-forum.com/.