Skip to content

Instantly share code, notes, and snippets.

View rubysolo's full-sized avatar

Solomon White rubysolo

View GitHub Profile
@rubysolo
rubysolo / fake_rails.rb
Created March 27, 2014 20:15
minimum viable rails
unless defined?(Rails)
# minimum viable implementation of Rails
class Rails
class << self
def root
Pathname.new(File.expand_path('../..', __FILE__))
end
def env
'test'.tap do |e|

Keybase proof

I hereby claim:

  • I am rubysolo on github.
  • I am rubysolo (https://keybase.io/rubysolo) on keybase.
  • I have a public key whose fingerprint is DAC3 8223 1D35 4CF9 65F4 DB74 4392 7357 9969 D06E

To claim this, I am signing this object:

defrecord Card, suit: nil, rank: nil
defmodule Cards do
def create do
lc suit inlist [:H, :D, :C, :S],
rank inlist [:A, 2, 3, 4, 5, 6, 7, 8, 9, 10, :J, :Q, :K] do
Card.new suit: suit, rank: rank, points: score(rank)
end
end
@rubysolo
rubysolo / tracy.coffee
Created December 16, 2013 18:10
add console log to each function invocation
falafel = require 'falafel'
fs = require 'fs'
raw = process.argv[2]
cooked = raw.replace(/\.js$/, '-trace.js')
counter = 0
src = fs.readFileSync(raw, encoding: 'utf8')
tracied = falafel src, (node) ->
module Enumerable
def remove(other)
base = self.dup
other.each { |i| base.delete_at(base.index(i) || base.length) }
base
end
end
irb(main):008:0> [1,1,2,3].remove([1,2])
=> [1, 3]
@rubysolo
rubysolo / spec_helper.rb
Created November 7, 2013 13:43
Workaround for rspec-fire "does not implement" errors with ActiveRecord models
# inside config.before(:suite)
ActiveRecord::Base.subclasses.each do |model_class|
columns = model_class.columns.map { |c| c.name.to_sym }
missing = columns - model_class.instance_methods
missing.each do |column|
model_class.send(:define_method, column) { |*args| super(*args) }
end
end
@rubysolo
rubysolo / ring.exs
Last active December 26, 2015 02:09
Ring of message passing processes in Elixir (see http://benjamintanweihao.github.io/blog/2013/10/09/elixir-by-example-ring/)
defmodule Ring do
def run(member_count, loop_count, message) do
log(:ringleader, "building ring with #{ member_count - 1 } more nodes...")
next_pid = spawn_link(__MODULE__, :build_ring, [member_count - 2, self])
log(:ringleader, "sending initial message to #{ inspect(next_pid) }...")
next_pid <- {:message, message, loop_count}
message_loop(next_pid)
end
collection = [:a, :b, :c]
collection.each_with_index.each_with_object({}) do |(item, index), hash|
hash[item] = index
end
# => {:a=>0, :b=>1, :c=>2}
# An experiment in higher-order (?) messages in Ruby.
class Message
attr_reader :name
attr_reader :arguments
def initialize(name, *arguments)
@name = name
@arguments = arguments
end
@rubysolo
rubysolo / view.sql
Created June 26, 2013 16:47
cfd query
SELECT d.day, s.description, SUM(1) AS count
FROM (SELECT current_date - days.day AS day FROM generate_series(0,
(SELECT CAST(EXTRACT(epoch FROM NOW() - DATE(MIN(start_date))) / 86400 AS integer) FROM pivotal_tracker_story_histories)
,1) AS days(day)) d INNER JOIN pivotal_tracker_story_histories h ON d.day BETWEEN h.start_date AND COALESCE(h.end_date, current_date)
INNER JOIN pivotal_tracker_story_statuses s ON s.id = h.status_id
GROUP BY d.day, s.description
ORDER BY d.day, s.description