Created
January 4, 2013 02:03
-
-
Save anonymous/4449313 to your computer and use it in GitHub Desktop.
12 days of Rubymas. Based on clojure implementation by @syntacticsugar
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 Fixnum | |
def ordinal | |
%w[first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth][self - 1] | |
end | |
# Use #to_word rather than #to_s, so it doesn't break any other printing of numbers. | |
def to_word | |
%w[one two three four five six seven eight nine ten eleven twelve][self - 1] | |
end | |
end | |
GIFTS = { | |
1 => "an embarrassing garbage collector with a stutter", | |
2 => "#ruby slippers", | |
3 => "hash rockets => => =>", | |
4 => "unplayed games", | |
5 => "Sinatra songs", | |
6 => "segfaulting extensions", | |
7 => "pragmatic hackers", | |
8 => "lucky stiffs", | |
9 => "scintillating beardies", | |
10 => "leet Macbooks", | |
11 => "gazillion tests", | |
12 => "Rails hipsters", | |
} | |
def gift(day) | |
# Memoise it because it is hip and we have hips. | |
# Completely pointless, but 'memoisation' is fun to type and fun to say! | |
# Don't let nasty spell-checkers make you 'memorise' it though D: | |
$gifts ||= Hash.new do |hash, key| | |
hash[key] = case key | |
when 1 then GIFTS[1] | |
when 2..12 then "#{key.to_word} #{GIFTS[key]}" | |
else raise "an exquisite eyebrow" | |
end | |
end | |
$gifts[day] | |
end | |
def list_of_gifts_recursive(day) | |
# Put the gifts in a big pile, ready for the courier service. | |
case day | |
when 1 then "#{gift 1}" | |
# Could recurse this next case, but it is as no simpler than just doing it | |
# this way (list_of_gifts(1) vs. gift(1)). | |
when 2 then "#{gift 2} and #{gift 1}" | |
when 3..12 then "#{gift(day)}, #{list_of_gifts_recursive(day - 1)}" | |
end | |
end | |
def list_of_gifts_functionalish(day) | |
# Put all but the last gift in a big pile, ready for the courier service. | |
gifts = day.downto(2).map(&method(:gift)).join(", ") | |
# And it was looking so functional... | |
gifts << " and " unless day == 1 | |
gifts + gift(1) | |
end | |
def list_of_gifts_functional(day) | |
# Put the gifts in a big pile, ready for the courier service. | |
# As we know, #injecting gives you cooties, so stay in school #with_object! | |
day.downto(1).with_object("") do |current_day, str| | |
str << gift(current_day) | |
case current_day | |
when 3..12 then str << ", " | |
when 2 then str << " and " | |
# when 1 then do nowt! | |
end | |
end | |
end | |
def chorus(day, algorithm) | |
list_of_gifts = send "list_of_gifts_#{algorithm}", day | |
puts "On the #{day.ordinal} day of Rubymas, my true love gave to me: #{list_of_gifts}." | |
puts | |
end | |
# Use three different algorithms to generate the same output. | |
[:recursive, :functionalish, :functional].each do |algorithm| | |
puts "=" * 40 | |
puts "Now let's be a good little #{algorithm} Al Gore Rhythm!" | |
puts | |
(1..12).each {|day| chorus day, algorithm } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment