简体   繁体   中英

private method 'eval' called for Math:Module (NoMethodError)

I can not figure out how to use this method. It seems really strange to me that it should be a private method within Math, as that would make it completely non-accessible for developers (as far as I can tell). What am I missing?

My code is below:

str = "(5+9)/(3+4)"
puts Math.eval(str)

I am not sure, what you are trying to achieve, but while str has a valid ruby syntax (the one from the example above has), here you go:

▶ str = "(5+9)/(3+4)"
#⇒ "(5+9)/(3+4)"
▶ instance_eval str
#⇒ 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM