Last active
April 24, 2017 09:10
-
-
Save shahryarjb/87be6d14841b78e3387802f8d3dbe698 to your computer and use it in GitHub Desktop.
This file contains 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
# my blog : https://trangell.com/fa/ | |
# public : http://iranonrails.ir | |
defmodule Attendee do | |
defstruct name: "", paid: false, over_18: true | |
def may_attend_after_party(attendee = %Attendee{}) do | |
attendee.paid && attendee.over_18 | |
end | |
def print_vip_badge(%Attendee{name: name}) when name != "" do | |
IO.puts "Very cheap badge for #{name}" | |
end | |
def print_vip_badge(%Attendee{}) do | |
raise "missing name for badge" | |
end | |
end | |
iex(1)> a1 = %Attendee{ name: "rails", over_18: true } | |
%Attendee{name: "rails", over_18: true, paid: false} | |
iex(2)> Attendee.may_attend_after_party(a1) | |
false | |
iex(3)> a2 = %Attendee{ name: "rails", over_18: true, paid: true } | |
%Attendee{name: "rails", over_18: true, paid: true} | |
iex(4)> Attendee.may_attend_after_party(a2) | |
true | |
iex(5)> Attendee.print_vip_badge(a2) | |
Very cheap badge for rails | |
:ok | |
iex(6)> Attendee.print_vip_badge(a2) | |
Very cheap badge for rails | |
:ok | |
iex(7)> a3 = %Attendee{ name: "", over_18: true, paid: true } | |
%Attendee{name: "", over_18: true, paid: true} | |
iex(8)> Attendee.print_vip_badge(a3) | |
** (RuntimeError) missing name for badge | |
defstruct1.exs:16: Attendee.print_vip_badge/1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment