Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# 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