Skip to content

Instantly share code, notes, and snippets.

@zaphod42
Created November 10, 2012 17:32
Show Gist options
  • Select an option

  • Save zaphod42/4051853 to your computer and use it in GitHub Desktop.

Select an option

Save zaphod42/4051853 to your computer and use it in GitHub Desktop.
Solution to the coin changer kata
class Object
def when_defined(primary, alternate)
primary.call(self)
end
end
class NilClass
def when_defined(primary, alternate)
alternate.call(self)
end
end
module Bank
module_function
COINS = [25, 10, 5, 1]
def change(amount)
COINS.
find { |coin| amount >= coin }.
when_defined ->(coin) { [coin] + change(amount - coin) },
->(coin) { [] }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment