Skip to content

Instantly share code, notes, and snippets.

View tjhanley's full-sized avatar
🏒

Thomas Hanley tjhanley

🏒
View GitHub Profile
@tjhanley
tjhanley / gist:4958681
Last active December 13, 2015 18:58
crap
Original Gem:
https://github.com/Lytro/multi_db/blob/master/lib/multi_db/connection_proxy.rb#L74
Alvin and My fix:
https://github.com/alvinlai/multi_db/blob/bugfix-load-wrong-slave-db/lib/multi_db/connection_proxy.rb#L74
see ^
look closer... diff:
https://github.com/alvinlai/multi_db/commit/3c5d15a3a9cd2d759f89426f290aef63f6758b77#lib/multi_db/connection_proxy.rb
@tjhanley
tjhanley / add_1_to_n.rb
Last active March 1, 2016 16:06
Simple Problems
def add_1_to_n(n)
return n <= 0 ? 0 : n + add_1_to_n( n - 1 )
end
puts add_1_to_n(10) #=> 55
@tjhanley
tjhanley / LinkedListTest
Last active December 14, 2015 18:39
Linked List in Ruby
$> ./linked_list.rb
Run options:
# Running tests:
......
Finished tests in 0.000599s, 10016.6945 tests/s, 20033.3890 assertions/s.
6 tests, 12 assertions, 0 failures, 0 errors, 0 skips
module BinaryTree
class Node
attr_reader :word, :count, :left, :right
include Enumerable
def initialize(word)
@word, @count = word, 1
end
@tjhanley
tjhanley / stock.rb
Created March 21, 2013 02:53
I thought about the problem a bit more and given we can assume it is a time range if I knew where the maximum number was in the array i could get that index and work backward. Once I thought of the problem that way and with the key statement you gave me to watch out for those ranges that could go up and down. A sample result would look like this…
#!/usr/bin/env ruby -wKU
require "test/unit"
class Stock
# To change this template use File | Settings | File Templates.
def self.find_longest_run(prices=[])
return {:low=>{0.0=>0},:high=>{0.0=>0}} if prices.nil? || prices.size == 0
result = {}
# start at the end
#find the highest value
@tjhanley
tjhanley / railswizard.rb
Created March 23, 2013 17:33
generic setup
# >---------------------------------------------------------------------------<
#
# _____ _ _ __ ___ _
# | __ \ (_) | \ \ / (_) | |
# | |__) |__ _ _| |___\ \ /\ / / _ ______ _ _ __ __| |
# | _ // _` | | / __|\ \/ \/ / | |_ / _` | '__/ _` |
# | | \ \ (_| | | \__ \ \ /\ / | |/ / (_| | | | (_| |
# |_| \_\__,_|_|_|___/ \/ \/ |_/___\__,_|_| \__,_|
#
# This template was generated by RailsWizard, the amazing and awesome Rails
@tjhanley
tjhanley / log_parse.rb
Last active December 15, 2015 10:49
parse out Mac OS versions from S3 Logs.
require "pp"
os_versions = {}
Dir['logs/*'].each do |path|
IO.readlines(path).each do |line|
%r/\b(?<os>mac os x \d{1,}_\d{1,}_\d{1,})\b|\b(?<os>osversion=\d{1,}\.\d{1,}\.\d{1,})\b/i =~ line
if os
os_versions[os] = os && os_versions[os] && os_versions[os] >= 1 ? os_versions[os] + 1 : 1
end
@tjhanley
tjhanley / setup.sh
Last active December 17, 2015 08:08 — forked from mrsweaters/setup.sh
#!/usr/bin/env bash
# login as root and run this script via bash & curl:
apt-get update
apt-get install -y build-essential bison openssl libreadline6 libreadline6-dev curl \
git-core zlib1g zlib1g-dev libopenssl-ruby curl libcurl4-openssl-dev libssl-dev \
libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev
bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
@tjhanley
tjhanley / user.rb
Created August 6, 2013 20:50
Need a better way....
def perform(user_id, class_name)
user = eval class_name + '.find(' + user_id.to_s + ')'
end
module AnotherCalc
def perform(product)
case product
when "firstcase"
do_something
when "secondcase"
do_something_else
when "thirdcase"
do_something_more
when "fourthcase"