Skip to content

Instantly share code, notes, and snippets.

View Adzz's full-sized avatar

Adam Lancaster Adzz

View GitHub Profile
@Adzz
Adzz / class_references.sh
Created August 8, 2016 17:01
This is lifted from Gary Bernhardt here: https://www.youtube.com/watch?v=sCZJblyT_XM It shows the number of times a class is referenced within an app
grep -rh '^[[:space:]]*\(class\|module\)\b' api/app api/lib --include='*.rb' | sed 's/^[[:space:]]*//' | cut -d ' ' -f 2 | while read class; do echo "`grep -rl "\b$class\b" api/app api/lib --include="*.rb" | wc -l` $class"; done | sort -n
@Adzz
Adzz / middle.sh
Created August 1, 2017 22:15
Trying to find a way to keep the prompt in the middle of the terminal
# see here: https://superuser.com/questions/1106674/how-to-add-blank-lines-above-the-bottom-in-terminal#_=_
# the n flag on echo eliminates new line, e flag interprets the exit codes
# echo -ne "\eD" moves down and scrolls the viewport
# echo -ne "\eM" moves up and scrolls the viewport if necessary
# "\e[B" and "\e[A" move up and down respectively constraining within the viewport
# for those two there are multipliers: "\e[3A" will repeat "\e[A" 3 times
# tput lines returns us the current number of rows in the viewport - i.e. how tall it is.
# this just doesnt seem to work
Square.area(%Square{side: 10})
Circle.area(%Circle{radius: 10})
defmodule Project do
def total_cost(shape, cost_per_square_meter) do
# code goes here ….
end
end
defmodule Project do
def total_cost(shape, cost_per_square_meter) do
case shape do
%Square{} -> Square.area(shape) * cost_per_square_meter
%Circle{} -> Circle.area(shape) * cost_per_square_meter
end
end
end
defmodule Project do
def total_cost(shape = %Square{}, cost_per_square_meter) do
Square.area(shape) * cost_per_square_meter
end
def total_cost(shape = %Circle{}, cost_per_square_meter) do
Circle.area(shape) * cost_per_square_meter
end
end
defmodule Project do
def total_cost(shape, cost_per_square_meter) do
Shape.area(shape) * cost_per_square_meter
end
end
defprotocol Shape do
def area(shape)
end
defimpl Shape, for: Square do
def area(shape) do
shape.side * shape.side
end
end
defimpl Shape, for: Circle do
def area(shape) do
shape.radius * shape.radius * 3.14
end
end