Skip to content

Instantly share code, notes, and snippets.

require 'date'
class ThreePaycheckCalculator
attr_reader :first_payday, :pay_interval, :number_of_paydays, :pay_days
def initialize(first_payday, pay_interval: 14, number_of_paydays: 100)
@first_payday = first_payday
@pay_interval = pay_interval
@number_of_paydays = number_of_paydays
@andycamp
andycamp / block_object_builder
Created April 6, 2016 13:04
Builds up object instances with blocks in a module (inspired by rack-attack)
module Prank
@@jokes = []
class Joke
def initialize(name, options, block)
@name = name
@options = options
@block = block
@andycamp
andycamp / truncate_db_task.rb
Created January 30, 2014 16:41
Rake Task for truncating all tables in a rails app. Preserves the schema_migrations.
desc "Truncate all tables in the database"
task :truncate_all_tables! => :environment do
unless Rails.env.production?
puts "TRUNCATING ALL TABLES in 3 seconds!!!!"
sleep 3
ActiveRecord::Base.connection.tables.each do |table|
unless table == "schema_migrations"
puts "Truncating #{table}."
ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{table} RESTART IDENTITY CASCADE;")
else
@andycamp
andycamp / cypher_calendar.cql
Created January 29, 2014 13:29
Calendar Graph Cypher
start c =node(14417) create unique (c)-[:year]->(y { year: 2014 })-[:month]->(m { month: 1})-[:day]->(d { day: 30 }) return c,y,m,d;
@andycamp
andycamp / cvs_append.rb
Last active January 1, 2016 17:59
Ruby CSV append
require 'csv'
CSV.open("test.csv", "w", :col_sep => "\t") do |csv|
csv << ["one", "two", "three"]
csv << [1, 2, 3]
csv << [1, 2, 3]
csv << [1, 2, 3]
end

tmux cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname
@andycamp
andycamp / method_swap.rb
Created November 9, 2013 11:26
Swap Class method
class Franklin
def self.speak
puts "Hello"
end
end
orig_speak_proc = Franklin.method(:speak)
@andycamp
andycamp / same_name_diff_extensions.rb
Created September 14, 2012 12:18
Same File Name but Different Extensions
def same_name_diff_extensions(search_val,arr)
arr.map{ |i| i unless i.match(%r{^#{search_val}\..*}).nil? }.compact
end
@andycamp
andycamp / one_hash_two_arrs.rb
Created September 14, 2012 11:26
Hash From Two Equivalent Arrays
a = ["one","two","three"]
b = ["un", "du", "trois"]
c = Hash[a.zip(b)]
puts c
# => {"one"=>"un", "three"=>"trois", "two"=>"du"}
@andycamp
andycamp / inheritable_constants.rb
Created August 18, 2012 13:16
ruby inheritable constants
class A
SUB_TYPES = %w(bass trout salmon)
def valid_sub_type?(sub_type)
self.class::SUB_TYPES.include? sub_type
end
end
class B < A
SUB_TYPES = %w(whale beaver dog)