Using pattern matching to compare two strings

ryanwilldev

Ryan Will

Posted on January 15, 2020

Using pattern matching to compare two strings

Elixir’s pattern matching feature is the gift that keeps on giving. I love this neat trick to check if two strings are the same. It is an easy way to check if a password and password confirmation match.

def passwords_match?(password, password), do: true

def passwords_match?(_, _), do: false

passwords_match?("password", "password") # true
passwords_match?("password", "notpassword") # false
💖 💪 🙅 🚩
ryanwilldev
Ryan Will

Posted on January 15, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

TIL: about Entity Relationship Schemas
TIL IEX Console #2
elixir TIL IEX Console #2

May 12, 2022

Pattern Match Struct's Name
elixir Pattern Match Struct's Name

January 21, 2020

Pattern match in with clause 🤯
elixir Pattern match in with clause 🤯

January 15, 2020