Skip to content

Instantly share code, notes, and snippets.

@bchase
Created July 15, 2017 23:44
Show Gist options
  • Save bchase/dfb03223b7332d04bc695956e62c2192 to your computer and use it in GitHub Desktop.
Save bchase/dfb03223b7332d04bc695956e62c2192 to your computer and use it in GitHub Desktop.
defmodule Foo do
def crash_unless_password(pw) do
"password" = pw
IO.puts "I didn't crash!"
end
def validate_v1(token) do
case token do
"tok_" <> _ ->
IO.puts "Valid token `#{token}!`"
_ ->
IO.puts "Invalid token `#{token}, bruh.`"
end
end
def validate_v2("tok_" <> _ = token), do: IO.puts "Valid token `#{token}!`"
def validate_v2(token), do: IO.puts "Invalid token `#{token}, bruh.`"
end
IO.puts Foo.validate_v1 "tok_good"
IO.puts Foo.validate_v1 "bad"
IO.puts Foo.validate_v2 "tok_good"
IO.puts Foo.validate_v2 "bad"
Foo.crash_unless_password "password"
Foo.crash_unless_password "boof"
# $ elixir matching.exs
# Valid token `tok_good!`
# ok
# Invalid token `bad, bruh.`
# ok
# Valid token `tok_good!`
# ok
# Invalid token `bad, bruh.`
# ok
# I didn't crash!
# ** (MatchError) no match of right hand side value: "boof"
# matching.exs:3: Foo.crash_unless_password/1
# (elixir) lib/code.ex:370: Code.require_file/2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment