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
require 'rational' | |
class Numeric | |
def to_r(rounding = 10_000) | |
Rational((self * rounding).to_i, rounding) | |
end | |
end | |
class Rational | |
def to_proper |
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
# jQuery style method chaining https://twitter.com/grossberg/status/1297223439 | |
class Thing | |
attr_accessor :name | |
def initialize(name) | |
self.name = name | |
end | |
def announce |
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
#!/usr/bin/ruby | |
files = {} | |
Dir.glob('**/*').each do |file| | |
basename = File.basename(file).downcase | |
if File.file?(file) | |
files[basename] ||= [] | |
files[basename] << file | |
end | |
end |
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
# Nginx cache busting rewriter, best used on assets that have long-lived expires. | |
# | |
# Rewrites like so: | |
# blah.com/release_ab2ea212312.../file.css => blah.com/file.css | |
# | |
location ~ ^/release_(.*?)/ { | |
rewrite ^/release_(.*?)/(.*)$ /$2 last; | |
} |
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
#!/usr/bin/env /some/rails/project/script/runner | |
# Shows every class that contains capitalized method names and the "offending" methods. | |
methods = {} | |
ObjectSpace.each_object(Class) do |klass| | |
capitalized_methods = klass.methods.select { |m| m =~ /[A-Z]/ } | |
unless capitalized_methods.empty? | |
methods[klass] = capitalized_methods | |
end |
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
def make_class(klass) | |
::Object.class_eval " | |
class #{klass} < #{self} | |
end | |
" | |
end |
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
88888! |
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
class FilterableEnumerable | |
def initialize(original) | |
@original = original | |
end | |
def >(value) | |
@original.select { |v| v > value } | |
end | |
def >=(value) |
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
def reindent(data, base) | |
lines = data.split("\n") | |
smallest_indentation = lines.collect { |l| l =~ /\w/ }.min | |
lines.each do |line| | |
line.gsub!(/^\s{#{smallest_indentation}}/, ' ' * base) | |
end | |
lines.join("\n") | |
end |
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
value = | |
10.times do |i| | |
if i == 5 | |
break "found five!" | |
end | |
end | |
value # => "found five!" |