Skip to content

Instantly share code, notes, and snippets.

View micahboyd's full-sized avatar

Micah Boyd micahboyd

  • Qantas Hotels
  • Melbourne, AUS
View GitHub Profile
@micahboyd
micahboyd / pg_ctl.sh
Created October 5, 2020 00:31
Terminate zombie postgres pid
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`
@micahboyd
micahboyd / advanced_case_statements.rb
Created September 27, 2020 23:43
Exploring case statement features in ruby
# 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)
@micahboyd
micahboyd / merge_sort.rb
Created September 13, 2019 01:12
Ruby Merge Sort
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)
@micahboyd
micahboyd / json_object_class.rb
Last active December 30, 2023 21:13
JSON #parse object_class
# 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
@micahboyd
micahboyd / attr_set.rb
Created April 29, 2019 23:24
Create give defalt values to attrs
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}") :
@micahboyd
micahboyd / reduce_with_object.rb
Last active August 15, 2018 02:25
Reduce vs each with_object
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
@micahboyd
micahboyd / method_lambda.rb
Created June 27, 2018 05:46
Ruby method lambda?
# 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
@micahboyd
micahboyd / ping_pong.ex
Created May 18, 2018 04:46
Elixir Ping Pong
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
@micahboyd
micahboyd / genserver_practic_2.ex
Created April 29, 2018 23:29
Elixir GenServer practice 2
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
@micahboyd
micahboyd / genserver_practice.ex
Created April 29, 2018 23:27
Elixir GenServer practice
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