This file contains hidden or 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
class Object | |
def method_missing m, *args | |
Object.respond_to?(m, true) ? Object.send(m, self, *args) : super | |
end | |
end |
This file contains hidden or 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
/* On Array */ | |
VALUE | |
rb_ary_each(ary) | |
VALUE ary; | |
{ | |
long i; | |
for (i=0; i<RARRAY(ary)->len; i++) { |
This file contains hidden or 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
import Data.List | |
lineBreak :: String -> String | |
lineBreak = joinBrokenLines . joinWordsInBrokenLines . brokenLines . words | |
brokenLines :: [String] -> [[String]] | |
brokenLines [] = [] | |
brokenLines wordList = brokenLine : brokenLines remainingWords | |
where (brokenLine, remainingWords) = splitAt (brokenLineWordCount wordList) wordList |
This file contains hidden or 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
# A poor man's complexity measure for a file or group of files | |
puts ARGF.lines.grep(/^([ \t]*)/) { $1 }.map(&:length).reduce(0, :+) |
This file contains hidden or 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
git log --name-only --no-merges | grep \.rb$ | sort | uniq -c | sort -nr |
This file contains hidden or 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
def complexity filename | |
File.read(filename).lines.grep(/^([ \t]*)/) { $1 }.map(&:length).reduce(0, :+) | |
end | |
commit_filenames = `git log --name-only | grep \.rb$`.lines \ | |
.map(&:strip) \ | |
.reject { |fn| fn.include? "_spec" } \ | |
.reject { |fn| fn.include? "vendor" } |
This file contains hidden or 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
def complexity filename | |
File.read(filename).lines.grep(/^([ \t]*)/) { $1 }.map(&:length).reduce(0, :+) | |
end | |
filenames = `git log --name-only | grep \.rb$`.lines \ | |
.map(&:strip) \ | |
.reject { |fn| fn.include? "_spec" } \ | |
.reject { |fn| fn.include? "vendor" } |
This file contains hidden or 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
require 'ostruct' | |
require 'date' | |
class OrdersReport < Struct.new(:orders,:date_range) | |
def total_sales_within_date_range | |
orders.select {|order| date_range.include?(order.placed_at) } | |
.map(&:amount) | |
.reduce(0.0,:+) | |
end | |
end |
This file contains hidden or 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
def zip_hash hash_a, hash_b, missing_element = nil | |
all_keys = (hash_a.keys + hash_b.keys).uniq | |
result = {} | |
all_keys.each do |key| | |
result[key] = [hash_a[key] || missing_element, hash_b[key] || missing_element] | |
end | |
result | |
end |
This file contains hidden or 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
# :: [[a,b]] -> (a..a) -> b -> [[a,b]]] | |
def spread mappings, range, default_value = 0 | |
occupied = Hash[mappings] | |
range.map { |index| [index, occupied[index] || default_value] } | |
end |