-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcase.exs
36 lines (31 loc) · 864 Bytes
/
case.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
defmodule Case do
@sean %{name: "Sean", age: 39}
def check_id(person \\ @sean) do
case person do
%{age: age} when is_number(age) and age >= 21 ->
IO.puts "Welcome. You can enter the bar, #{person.name}."
_ ->
IO.puts "Sorry #{person.name}, you must be 21 or older to enter."
end
end
def ok!(response) do
case response do
{:ok, data} ->
data
{:error, reason} ->
raise RuntimeError, message: to_string(reason)
end
end
def open_file!(filename \\ "cats.txt") do
ok! File.open(filename)
end
def open_file(filename \\ "cats.txt") do
case File.open(filename) do
{:ok, file} ->
line = String.rstrip(IO.read(file, :line), ?\n)
IO.puts "First line: #{line}"
{:error, reason} ->
IO.puts "Failed to open file: #{reason}"
end
end
end