Skip to content

Instantly share code, notes, and snippets.

@spencerroan
spencerroan / interface_proxy.rb
Created January 4, 2012 20:06
wrap an object in a proxy that will report what methods are called, from where and what is returned.
class InterfaceProxy
def self.create(delegate)
begin
#check if proxy exists
if delegate.__class == self
delegate.log "proxy previously created, returning delegate"
return delegate
end
rescue
# if not return a new one
@spencerroan
spencerroan / primes.rb
Last active August 29, 2015 14:16
playing with a prime number seive in ruby
# http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
class Primes
def self.time_it
t = Time.now
rval = yield
elapsed = Time.now - t
#return rval, elapsed
puts elapsed
return rval
@spencerroan
spencerroan / ruby2.2.2.txt
Created May 8, 2015 16:24
upgrading to ruby 2.2.2
rvm install ruby-2.2.2
bundle install
#rspec spec failed on redis
brew install redis
# turn on redis at login
mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents
# turn on redis now!
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
@spencerroan
spencerroan / 3.2.rb
Created May 14, 2015 15:10
confident ruby 3.2 examples
# tests for 3.2 of confident ruby
# inplicit vs explicit
#
require 'pry'
describe "section 3.2" do
context "string" do
class C
attr_accessor :name
def remove_membership(membership)
ii = Stripe::InvoiceItem.retrieve(membership.stripe_invoice_item)
ii.delete
rescue
#nothing
end
def kill_stripe(n=30)
query = Membership.where(Membership.arel_table[:updated_at].gt 33.days.ago).where.not(stripe_invoice_item: nil)
count = query.count
def lines(invoice)
(invoice.lines.count / 100.0).ceil.times.map do |page|
offset = page * 100
invoice.lines.all(count: 100, offset: offset).map do |line|
Stripe::InvoiceItem.construct_from line
end
end.flatten
end
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 1 column, instead of 2 in line 1.
create temp table hydrated_temp (
occurred_on date,
user_id int,
company_id int);
with
participation as (
select occurred_on, user_id from simple_activities
union
select occurred_on, user_id from simple_sleeps
We can make this file beautiful and searchable if this error is corrected: It looks like row 3 should actually have 1 column, instead of 3 in line 2.
with current_goals as (
select g.user_id, g.id, 'eat_move_sleep' as goal_type
from eat_move_sleep_goals g
where now() between start_date and end_date
union
select g.user_id, g.id, 'weight_loss' as goal_type
from weight_loss_goals g
where now() between start_date and end_date
union
with hydrated_sleeps as (
select ss.user_id, ss.well_rested, ss.occurred_on, u.company_id, u.gender
from simple_sleeps ss inner join users u on u.id = ss.user_id
),
filtered_sleeps as (
select * from hydrated_sleeps where company_id = 12 or company_id = 4
),
sleep_counts as (
select
with hydrated_activities as (
select t.user_id, t.intensity, t.occurred_on, u.company_id, u.gender
from simple_activities t inner join users u on u.id = t.user_id
),
filtered_activities as (
select * from hydrated_activities where company_id = 12 or company_id = 4
),
activity_counts as (
select