Skip to content

Instantly share code, notes, and snippets.

class Reader
def read
return get.chomp()
end
end
class Writer
def write(chars)
puts chars
end
class Reader
def read
file = File.new("readfile.rb", "r")
return file.gets
end
end
class Writer
def write(chars)
file = File.new("readfile.rb", "w")
@patrickgombert
patrickgombert / gist:2371209
Created April 12, 2012 21:44
Interactor Example
### We are highlighting a workflow to retrieve Employees who
### have the title of department leaders.
### The web entry point comes through the controller,
### but it's important for the controller to have no logic.
### It simply knows that it must call the department leaders
### method on the interactor.
class EmployeeController < ApplicationController
def get_department_leaders
leaders = Interactor::Employees.get_deparment_leaders
@patrickgombert
patrickgombert / 8LASCII
Created September 28, 2012 18:40
8th Light ASCII Art
MMMM MMM
MM: 8M MMM$I7
MM: OM MMM7I7MM
MM, $M MMD7IIDMM
MM, 7M MM8III8M
MO. ?MM MMOIIIOM
M$ =MM MMMZIIIZMM
M7 =MM MMM$III$MM
MM? :MM MMM7III7MM
MM+ :MM MMN7III7M
@patrickgombert
patrickgombert / gist:4694122
Created February 1, 2013 21:02
Spec helper timer saver
require 'timer'
unless defined?(REQUIRED_SPEC_HELPER)
REQUIRED_SPEC_HELPER = true
File.open("spec_helper_times.yml", 'a+') do |file|
date_time = DateTime.now.strftime("%m%d%y%H%M")
time = Time.now
Kernel.load("spec_helper.rb", true)
final_time = Time.now - time
file << "#{date_time}: #{final_time}\n"
@patrickgombert
patrickgombert / gist:4694802
Last active December 12, 2015 01:58
erlang coin changer with absolute priority premise scoring
change(Amount) -> % => Binding 2 x 1
{Coins, _} = lists:foldl(fun(Denom, {Coins, Total}) -> % => Invocation 2 x 1, Binding 4 x 1, Constant 1 x 1
Floor = floor(Total / Denom), % => Binding 1 x 1, Invocation 2 x 2
{Coins ++ lists:duplicate(Floor, Denom), Total - (Floor * Denom)} % => Invocation 2 x 4
end, {[], Amount}, [25, 10, 5, 1]), % => Constant 2 x 1
Coins.
% Total = 2 + 2 + 4 + 1 + 1 + 4 + 8 + 2 = 24
anwer(foo, Args) -> % pattern match the arguments
answer_to_foo; % notice the semi-colon
answer(bar, Args) ->
case Delicious of
veggies ->
false; % Again, notice the semi colon
bacon -> % case is another form of pattern matching
true % The last statement in the case has no punctuation
end;
@patrickgombert
patrickgombert / gist:5470573
Created April 26, 2013 21:28
Key/Val Store in Elixir
defmodule Db do
use GenServer.Behaviour
## API
def start() do
:gen_server.start({:local, :db}, __MODULE__, [], [])
end
def store(key, val) do
@patrickgombert
patrickgombert / gist:6205156
Created August 11, 2013 14:33
Indestructable Erlang REPL
-module(shell).
-export([loop/0, eval_loop/0, eval/2]).
read() ->
io:get_line("> ").
eval(String, Bindings) ->
{ok, Tokens, _} = erl_scan:string(String),
{ok, Parsed} = erl_parse:parse_exprs(Tokens),
@patrickgombert
patrickgombert / gist:6393923
Created August 30, 2013 20:23
Dynamo 404 Question
post "/:something" do
conn = conn.resp_content_type("text/plain")
conn = conn.resp_body("This does not exist")
conn = conn.status(404)
conn
end
test "it returns a 404" do
conn = post("/not-real")