This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The peculiarities of #all?, #any?, #empty?, #none? | |
ary = [] | |
ary.all? # => true | |
ary.any? # => false | |
ary.empty? # => true | |
ary.none? # => true | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
####################### | |
# | |
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
reek | |
https://github.com/troessner/reek | |
cane | |
https://github.com/square/cane | |
churn | |
https://github.com/danmayer/churn | |
metric_fu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
OlderNewer