Skip to content

Instantly share code, notes, and snippets.

View pricees's full-sized avatar

Ted Price pricees

View GitHub Profile
@pricees
pricees / gist:8963659
Created February 12, 2014 20:15
Extending an Array/ActiveRelation vs Subclassing Array
names = %w[SK Steve Pedro Anthony Milo Xiping]
Person = Struct.new :name
module Helloable
def hello_all_the_names
each { |person| puts "Helloable | Hello #{person.name}" }
end
end
# An interesting way to add functionality to an array is to extend it
@pricees
pricees / Ruby Basic
Last active August 29, 2015 13:56
My Ruby Litmus Test.rb
# Integer vs Float div by zero gotchas
#
#
1 / 0 = [answer]
1.0 / 0 = [answer]
1 / 0.0 = [answer]
0 / 0.0 = [answer]
# String concatenation vs interpolation
x = 5
@pricees
pricees / filter_array_of_structs.rb
Created April 16, 2014 16:34
Filtering array structs by attributes
module Where
def where(options)
cols = options.keys
vals = options.values
select { |item| item.to_h.values_at(*cols) == vals }
end
end
Array.send :include, Where
@pricees
pricees / all_any_empty_none.rb
Created May 15, 2014 20:08
Ruby enumerable: #all? #any? #empty? #none?
# The peculiarities of #all?, #any?, #empty?, #none?
ary = []
ary.all? # => true
ary.any? # => false
ary.empty? # => true
ary.none? # => true
@pricees
pricees / symbolize_all_the_keys!.rb
Last active August 29, 2015 14:01
Symbolize All The Keys!
#
# Symbolize All The Keys....forreealz
class Hash
def symbolize_keys
inject({}) do |result, (key, v)|
key = key.to_sym rescue key
if v.is_a?(Hash)
result[key] = v.symbolize_keys
@pricees
pricees / gnarly_quicksort.rb
Last active August 29, 2015 14:01
Gnarly quicksort method to be written in Go
$ cat qs.rb
#
# How the merge sort is to be written for a Coursera course I am taking
@comps = 0
@stack = 0
def qs(a = [], l = 0, r = nil)
#puts "@stack = #{@stack += 1}"
@pricees
pricees / kargers_min_cut_algorithm.rb
Created May 30, 2014 13:16
Karger's min cut algorithm
#######################
#
# While there are more than 2 vertices:
# - pick a remaining edge (u, v) uniformly at random
# - merge edge (u, v) into a single vertex
# - remove self-loops
def deep_copy(original)
Marshal.load Marshal.dump(original)
@pricees
pricees / kargers_min_cut_algorithm.rb
Created June 1, 2014 03:00
Kargers Min Cut algorithm.....
# Input should be whitespace separated input where the first column is the vertex, and the remaining input is a list of vertices # it shares edges with.
# 1 2 3 4 vertex 1 shares an edge with 2, 3 and 4
# 2 1 4 vertex 2 shares an edit with 1, 4
trials = (ENV["T"] || 1).to_i
#######################
#
# While there are more than 2 vertices:
# - pick a remaining edge (u, v) uniformly at random
@pricees
pricees / gist:67dbdd989bbae58ee0f1
Created June 22, 2014 15:59
Static analysis gems and others I should be using more.
reek
https://github.com/troessner/reek
cane
https://github.com/square/cane
churn
https://github.com/danmayer/churn
metric_fu
@pricees
pricees / bowling_scores.rb
Last active August 29, 2015 14:03
Straight forward solution to scoring a blowing game
rolls = Array.new(21, 5)
p rolls
frame = 0
scores = []
while (f = rolls.shift) # first roll
frame += 1
if f == 10 # strike!
scores << f + rolls[0].to_i + rolls[1].to_i
else