Skip to content

Instantly share code, notes, and snippets.

View hoverlover's full-sized avatar

Chad Boyd hoverlover

View GitHub Profile
@hoverlover
hoverlover / enumerable_constants.rb
Created May 25, 2010 16:02
Module to facility the enumeration of constants
# This module should be used for situations where enumerating over constant values is desired.
# This facilitates keeping the code DRY by keeping the constant values defined in one place, and
# still having the ability to enumerate over them wherever they are needed.
#
# Example use cases:
#
# * defining constants for possible field values in an AR object and including this module to
# provide access to the values in a 'validates_inclusion_of' validation
# * defining constants for select box values in a view and including this module to allow them to be
# enumerated over in the select tag
# Pick up to 10 names, making sure to draw from each "hat" at least once
class Name < Struct.new(:first, :last); end
max_names = 10
hat1 = [Name.new('Abraham', 'Lincoln'), Name.new('George', 'Washington')]
hat2 = [Name.new('Thomas', 'Jefferson'), Name.new('James', 'Madison'), Name.new('Theodore', 'Roosevelt'), Name.new('John', 'Kennedy')]
hat3 = [Name.new('Calvin', 'Coolidge'), Name.new('Ronald', 'Regan'), Name.new('Woodrow', 'Wilson')]
[hat1, hat2, hat3].sort{|a,b| b.size <=> a.size}.inject{|memo, names| memo.zip(names)}.flatten.compact[0, max_names]
# open the file containing the gems to load and install each
File.open(Dir.pwd << "/gems.txt", "r") do |f|
while (line = f.gets)
puts `sudo gem install --no-ri --no-rdoc #{line}` unless line.strip.empty?
end
end