Skip to content

Instantly share code, notes, and snippets.

@ripesunflower
Last active January 20, 2016 19:30
Show Gist options
  • Save ripesunflower/2aaf68a62be7ef727548 to your computer and use it in GitHub Desktop.
Save ripesunflower/2aaf68a62be7ef727548 to your computer and use it in GitHub Desktop.
Calculating with Functions | Codewars
[:zero, :one, :two, :three, :four, :five,
:six, :seven, :eight, :nine].each.with_index do |method, index|
define_method method do |function = nil|
(function) ? function.call(index) : index
end
end
[[:plus, :+], [:minus, :-],
[:times, :*], [:divided_by, :/]].each do |(method, action)|
define_method method do |second_operand|
->(first_operand) { first_operand.send(action, second_operand) }
end
end
seven(times(five)) # => 35
four(plus(nine)) # => 13
eight(minus(three)) # => 5
six(divided_by(two)) # => 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment