The way it is written, that's not valid ruby syntax, so it can't be eval
ed.
You have two options:
a) Don't use eval. I would think that this is the best option, but I supposed that depends on the context.
b) Change the syntax to 5.a + 6.b + 3.a + 11.b
and define appropriate a and b methods, like so:
class Expr
attr_accessor :a,:b
def initialize(a,b)
@a, @b = a,b
end
def +(other)
Expr.new(a + other.a, b + other.b)
end
def inspect
"<expr: #{a}a + #{b}b>"
end
end
class Numeric
def a
Expr.new(self, 0)
end
def b
Expr.new(0, self)
end
end
5.a + 6.b + 3.a + 11.b #=> <expr: 8a + 17b>