Skip to content

Instantly share code, notes, and snippets.

@volonterx
Last active December 25, 2015 05:19
Show Gist options
  • Save volonterx/6923605 to your computer and use it in GitHub Desktop.
Save volonterx/6923605 to your computer and use it in GitHub Desktop.
#http://www.codewars.com/dojo/katas/5235c913397cbf2508000048/discuss/ruby
class Calculator
def evaluate(string)
groups_of_operators = [["/", "*"], ["-", "+"]]
arr = string.split
groups_of_operators.each do |group|
(0..arr.size).each do |i|
if group.include?(arr[i+1])
arr[i+1] = arr[i].to_i.send(arr[i+1], arr[i+2].to_i)
arr.delete_at(i+2)
arr.delete_at(i)
end
end
end
arr[0].to_i.send(arr[1], arr[2].to_i)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment