Skip to content

Instantly share code, notes, and snippets.

@ahmetabdi
Last active September 19, 2017 15:19
Show Gist options
  • Save ahmetabdi/f2258fdd2b3260a0e8404ec0d4be8c5c to your computer and use it in GitHub Desktop.
Save ahmetabdi/f2258fdd2b3260a0e8404ec0d4be8c5c to your computer and use it in GitHub Desktop.
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