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
class FilterableEnumerable | |
def initialize(original) | |
@original = original | |
end | |
def ==(value) | |
@original.select { |v| v == value } | |
end | |
def method_missing(sym, *args, &block) |
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!" |
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
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update( | |
# 4/5/9 | |
:mdy => proc { |t| t.strftime('%m/%d/%y').gsub /(\b)0/, '\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
# 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. |
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 equalize_lists(one, two, empty_list_item = '<li> </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 |
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 UserNotifier < ActionMailer::Base | |
def self.without_deliveries | |
initial = self.perform_deliveries | |
self.perform_deliveries = false | |
yield | |
self.perform_deliveries = initial | |
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
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 |
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 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 = {} |