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
When `brew services stop postgres` does not work try: | |
`pg_ctl -D /usr/local/var/postgres start` | |
`pg_ctl -D /usr/local/var/postgres stop` |
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
# Regular case statement: | |
case question | |
when :yes then 'Yep' | |
when :no then 'Nope' | |
end | |
# what this is calling === with each element in the case statement as an argument. | |
# basically question === :yes, or without the sugar: question.===(:yes) |
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
def merge_sort(array) | |
return array if array.length <= 1 | |
middle = array.length / 2 | |
left = merge_sort(array[0...middle]) | |
right = merge_sort(array[middle..]) | |
merge(left, right) | |
end | |
def merge(left, right) |
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
# Capture JSON into custom object similar to OpenStruct | |
class JsonClass | |
def []=(key, value) | |
instance_variable_set("@#{key}", new_member!(key, value)) | |
end | |
def [](key) | |
instance_variable_get("@#{key}") | |
end |
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
module AttrSet | |
def attr_set(options = {}) | |
options.each do |name, default_value| | |
define_method("#{name}=") do |new_value| | |
instance_variable_set("@#{name}", new_value) | |
end | |
define_method(name) do | |
instance_variable_defined?("@#{name}") ? instance_variable_get("@#{name}") : |
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
range = (1..10) | |
range.reduce([]) do |sum, x| | |
(sum << x + 2) if x.even? # doesn't work! | |
end | |
range.reduce([]) do |sum, x| | |
x.even? ? (sum << x + 2) : sum # works... | |
end |
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
# lambda way | |
adder = -> (x,y) { x + y } | |
math = -> (fn, x, y) { fn.(x,y) } | |
math.(adder, 1, 2) # => 3 | |
# method lambda?? | |
adder = method def adder(x,y) | |
x + y | |
end |
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
defmodule PingPong do | |
def start do | |
ping_process = spawn(Ping, :loop, []) | |
pong_process = spawn(Pong, :loop, []) | |
send(ping_process, {:pong, pong_process}) | |
end | |
def ping do | |
Ping | |
end |
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
defmodule KeyValueStore do | |
use GenServer | |
def start, do: GenServer.start(KeyValueStore, nil) | |
def put(pid, key, value) do | |
GenServer.cast(pid, {:put, key, value}) | |
end | |
def get(pid, key) do |
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
defmodule ServerProcess do | |
def start(callback_module) do | |
spawn(fn -> | |
initial_state = callback_module.init | |
loop(callback_module, initial_state) | |
end) | |
end | |
def call(server_pid, request) do |
NewerOlder