Last active
September 19, 2017 15:19
-
-
Save ahmetabdi/f2258fdd2b3260a0e8404ec0d4be8c5c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
value_matrix = { "zero" => 0, | |
"one" => 1, | |
"two" => 2, | |
"three" => 3, | |
"four" => 4, | |
"five" => 5, | |
"six" => 6, | |
"seven" => 7, | |
"eight" => 8, | |
"nine" => 9 } | |
value_matrix.each do |key, value| | |
define_method(key.to_sym) do |method = nil| | |
calculate(method, value) | |
end | |
end | |
symbol_matrix = { "plus" => "+", | |
"minus" => "-", | |
"times" => "*", | |
"divided_by" => "/" } | |
symbol_matrix.each do |key, value| | |
define_method(key.to_sym) do |amount| | |
"#{value} #{amount}.to_f" | |
end | |
end | |
def calculate(method, value) | |
method ? eval("#{value} #{method}") : value | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment