So i am learning Ruby and got stuck in one exercise. I need find a mistery number in a equation that is in a string.For example ('10 + ? = 18'). The course i am following ask to use eval. I know eval is dangerous, is just for learning purpose. I am using rspec to test, so dont need use if and return
All i got is this
class MisteryNumber def calculate(operation) count = 0 expression = operation.split(' = ') expression[0].gsub!("?", "#{count}") (eval(expression[0] - eval(expression[1]) end end
this code only works if i have subtraction or addition equations. If the equation is like this ('100 / 5 * ? = 40'), doesnt work anymore. I tried to do some form of iterator using gsub(thats why i have the 'count' in the code) but no luck. How i could od this?
https://stackoverflow.com/questions/67028861/find-a-mistery-number-in-a-equation-in-ruby April 10, 2021 at 06:09AM
没有评论:
发表评论