Skip to content

Instantly share code, notes, and snippets.

View ktravers's full-sized avatar
🌊🌊🌊

Kate Travers ktravers

🌊🌊🌊
View GitHub Profile
@ktravers
ktravers / account.ex
Last active July 12, 2018 03:11
Account with custom guard clauses
defmodule Account do
import Account.Guards, only: [is_private: 2]
def display_name(%{first: first, last: last, email: email}) when is_private(first, email) do
"<<Redacted>>"
end
def display_name(%{first: first, last: last}) when not is_nil(first) do
String.trim("#{first} #{last}")
end
def display_name(%{username: username}) when not is_nil(username) do
# Not recommended: macros
defmodule Account.Guards do
defmacro is_private(first_name, email) do
quote do
not(is_nil(unquote(first_name))) and
not(unquote(email) == unquote(first_name))
end
end
end
@ktravers
ktravers / account.ex
Created July 12, 2018 03:06
Guard clauses
defmodule Account do
def display_name(%{first: first, last: last}) when not is_nil(first) do
String.trim("#{first} #{last}")
end
def display_name(%{username: username}) when not is_nil(username) do
"#{username}"
end
def display_name(_), do: "New User"
end
@ktravers
ktravers / account.ex
Last active July 12, 2018 03:05
Unwieldy nil checks in Elixir
defmodule Account do
# Unwieldy nil checks
def display_name(%{first: nil, last: nil, username: nil}) do
display_name(%{})
end
def display_name(%{first: nil, last: nil, username: username}) do
display_name(%{username: username})
end
def display_name(%{first: nil, last: nil}), do: display_name(%{})
defmodule Account do
def display_name(%{first: first, last: last}) do
String.trim("#{first} #{last}")
end
def display_name(%{username: username}), do: "#{username}"
def display_name(_), do: "New User"
end
def display_name(user)
if user.first_name.length > 0
if user.last_name.length > 0
"#{user.first_name} #{user.last_name}".strip
else
"#{user.first_name}".strip
end
elsif user.username.length > 0
user.username
else
export const displayName = (user) => {
if (user.firstName.length > 0) {
if (user.lastName.length > 0) {
return `${user.firstName} ${user.lastName}`.trim();
} else {
return `${user.firstName}`.trim();
}
} else if (user.username.length > 0) {
return user.username;
} else {

Open your Mac Terminal and run the following commands.

Hint: you can run the commands by copying and pasting them into your terminal, then hitting the Enter/Return key.

Let It Snow

ruby -e 'C=`stty size`.scan(/\d+/)[1].to_i;S=["2743".to_i(16)].pack("U*");a={};puts "\033[2J";loop{a[rand(C)]=0;a.each{|x,o|;a[x]+=1;print "\033[#{o};#{x}H \033[#{a[x]};#{x}H#{S} \033[0;0H"};$stdout.flush;sleep 0.1}'
@ktravers
ktravers / database.yml
Created September 17, 2016 23:13
default database yml
default: &default
adapter: postgresql
pool: 5
timeout: 5000
encoding: unicode
development:
<<: *default
database: app_name_development