Skip to content

Instantly share code, notes, and snippets.

View jqr's full-sized avatar

Elijah Miller jqr

View GitHub Profile
class FilterableEnumerable
def initialize(original)
@original = original
end
def >(value)
@original.select { |v| v > value }
end
def >=(value)
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
class FilterableEnumerable
def initialize(original)
@original = original
end
def ==(value)
@original.select { |v| v == value }
end
def method_missing(sym, *args, &block)
value =
10.times do |i|
if i == 5
break "found five!"
end
end
value # => "found five!"
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(
# 4/5/9
:mdy => proc { |t| t.strftime('%m/%d/%y').gsub /(\b)0/, '\1' }
)
# Spawned from this thread: http://rubyflow.com/items/1990
module Enumerable
# Wraps an enumerable in an easy interface for selecting subsets. Comparison
# and method calls are supported.
#
# [0, 1, 2, 3].filter > 1 # => [2, 3]
# [0, 1, 2, 3].filter.zero? # => [0]
#
# Warning: Do not use this with the != operator, use filter_not instead.
def equalize_lists(one, two, empty_list_item = '<li>&nbsp;</li>')
difference = one.size - two.size
if difference == 0
[one, two]
elsif difference > 0
[one, two + [empty_list_item] * difference]
elsif difference < 0
[one + [empty_list_item] * difference.abs, two]
end
end
class UserNotifier < ActionMailer::Base
def self.without_deliveries
initial = self.perform_deliveries
self.perform_deliveries = false
yield
self.perform_deliveries = initial
end
end
A friend started donating to each open source project he uses on client
work[1], and I’m trying to follow the same practice. Except you don’t
seem to have pledgie enabled on any of your projects, gah! To each his
own, but please let me know if you enable pledgie.
[1] http://opensoul.org/2009/1/9/give-back-to-the-community
class Hash
# Flattens a hash by joining keys with joiner. Meant for use with String or
# Symbol keys. Resulting hash keys take on the type of the parent key.
#
# { 'a' => { 'b' => 1 } }.flatten_with_joined_keys
# # => {"a_b"=>1}
# { :a => { :b => { :c => { :d => 1 } } } }.flatten_with_joined_keys('')
# # => {:abcd=>1}
def flatten_with_joined_keys(joiner = '_')
output = {}