anakin
anakin

Reputation: 365

Is there an eval in ruby

Does ruby have something like pythons eval? I'm searching google and I don't know if it's how i'm searching but I can't find anything on it or how to use it if there is one

In python I would something like this

def doEval(object):
    return repr(eval(object))

The code above you would do in python's idle or something and run doEval('print("hello")') it would print "hello" then return "None" to say it was executed, in ruby I don't mind if it doesn't do nil, but I would like it to eval

Anything like this in ruby? thanks.

Upvotes: 0

Views: 1467

Answers (2)

konnigun
konnigun

Reputation: 1805

Try this:

command = "puts 1"
eval(command)

http://www.ruby-doc.org/core-2.0.0/Kernel.html#method-i-eval

Upvotes: 2

user2357112
user2357112

Reputation: 280291

Googling "ruby eval" quickly reveals that the answer is yes.

eval(string [, binding [, filename [,lineno]]]) → obj

Evaluates the Ruby expression(s) in string. If binding is given, which must be a Binding object, the evaluation is performed in its context. If the optional filename and lineno parameters are present, they will be used when reporting syntax errors.

Upvotes: 6

Related Questions