Skip to content

Instantly share code, notes, and snippets.

@KamilLelonek
Created September 7, 2015 16:58
Show Gist options
  • Save KamilLelonek/a5ae82e8786e34ab43b6 to your computer and use it in GitHub Desktop.
Save KamilLelonek/a5ae82e8786e34ab43b6 to your computer and use it in GitHub Desktop.
For a given integer return a result of dividing 8 by that number with handling 0.
Contract Num, Func[Num => Any], Proc => Any
def eight_divide_by(number, success_action, failure_action)
case number
# ...
when 4 then success_action[2]
when 2 then success_action[4]
when 1 then success_action[8]
when 0 then failure_action.call
end
end
success_action = lambda { |value| "Got #{value}" }
failure_action = lambda { "You probably wanted to divide by 0, didn't you?" }
eight_divide_by(1, success_action, failure_action) # => Got 8
eight_divide_by(0, success_action, failure_action) # => You probably wanted to divide by 0, didn't you?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment