Last active
October 26, 2016 16:54
-
-
Save brweber2/26c834effd8c3c17e19c2009381db514 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
# Create a variable has_seen_star_wars and set it to true. | |
has_seen_star_wars = true | |
# Write code that prints "Ready for class" if you have seen at least one Star Wars movie (has_seen_star_wars). | |
if has_seen_star_wars do | |
IO.puts "Ready for class" | |
end | |
# Flip has_seen_star_wars to false. | |
has_seen_star_wars = false | |
# Write code that prints "Ready for class" if you have seen at least one Star Wars movie (has_seen_star_wars). Otherwise print "Not ready". | |
if has_seen_star_wars do | |
IO.puts "Ready for class" | |
else | |
IO.puts "Not ready" | |
end | |
# Set the variable i_am_a_fan based on the if expression. | |
i_am_a_fan = if has_seen_star_wars do | |
true | |
else | |
false | |
end | |
# or more simply | |
i_am_a_fan = has_seen_star_wars | |
# Set the variable inside the if expression. What happens? Why? Should you do this? | |
if has_seen_star_wars do | |
i_am_a_fan = true | |
else | |
i_am_a_fan = false | |
end | |
# Set the variable to the return value of the if expression. What happens? Why? Should you do this? | |
i_am_a_fan = if has_seen_star_wars do | |
true | |
else | |
false | |
end | |
# case examples | |
# bad | |
case has_seen_star_wars do | |
true -> i_am_a_fan = true | |
_ -> i_am_a_fan = false | |
end | |
# good | |
i_am_a_fan = case has_seen_star_wars do | |
true -> true | |
_ -> false | |
end | |
# cond examples | |
# bad | |
cond do | |
has_seen_star_wars == true -> i_am_a_fan = true | |
true -> i_am_a_fan = false | |
end | |
# good | |
i_am_a_fan = cond do | |
has_seen_star_wars == true -> true | |
true -> false | |
end | |
# Write a function that will raise an error depending on the inputs. | |
defmodule MyModule do | |
def my_fun(a,b) do | |
a / b | |
end | |
end | |
# Call the function and trigger the error. | |
MyModule.my_fun(10,0) | |
# Call the function and do not trigger the error. | |
MyModule.my_fun(10,2) | |
# Add error handling to the function so it does not fail if an error occurs. | |
defmodule MyModule do | |
def my_fun(a,b) do | |
try do | |
a / b | |
rescue | |
e in [ArithmeticError] -> 0 | |
end | |
end | |
end | |
# Write a function that always prints out "Star Wars is awesome" even if it raises an error. | |
defmodule MyModule do | |
def my_fun(a,b) do | |
try do | |
a / b | |
after | |
IO.puts "Star Wars is awesome" | |
end | |
end | |
end | |
# Write a function that raises an error, handles it and logs the stacktrace in a friendly manner. | |
require Logger | |
defmodule MyModule do | |
def my_fun(a,b) do | |
try do | |
a / b | |
rescue | |
e in [ArithmeticError] -> | |
s = System.stacktrace | |
friendly_stack = Exception.format_stacktrace(s) | |
Logger.error("Sadly, an arithmetic error has occurred. #{friendly_stack}") | |
0 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment