Revisions
-
bil-bas revised this gist
Jan 7, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -85,7 +85,7 @@ def list_of_gifts_functionalish(day) # And it was looking so functional... gifts << ' and ' unless day == 1 gifts << gift(1) end def list_of_gifts_functional(day) -
bil-bas revised this gist
Jan 7, 2013 . 1 changed file with 89 additions and 61 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -9,87 +9,115 @@ def to_word end end class Rubymas include Enumerable GIFTS = { 1 => 'a garbage collector with an embarrassing 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', } # @param algorithm [:functional, :functionalish, :recursive] Algorithm used to create list of gifts. def initialize(algorithm) # Map the name of the algorithm to the appropriate method to use. list_of_gifts_method = "list_of_gifts_#{algorithm}" if respond_to? list_of_gifts_method @list_of_gifts = method list_of_gifts_method else raise ArgumentError, "Algorithm, #{algorithm.inspect} not supported" end end # Allow us to iterate through the song, with the Enumerable methods, # such as #each, #map, #reverse_each and #to_a. def each (1..12).each {|day| yield chorus day } end # Output as a full song, with each chorus separated by an empty line. def to_s to_a.join "\n\n" end # Let's stop naughty girls from peeking at their gifts too early! protected 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. # Lamdas are cool too. They are so cute and cuddly and fluffy. separator = lambda do |i| case i when 3..12 then ', ' when 2 then ' and ' when 1 then '' end end # 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)) << separator[current_day] end end def chorus(day) "On the #{day.ordinal} day of Rubymas, my true love gave to me: #{@list_of_gifts[day]}." end end # Use three different algorithms to generate identical output. This is the way # they do it in the space shuttle, you know. Ironically, it is 'redundancy' like # this that keeps people in work! [:recursive, :functionalish, :functional].each do |algorithm| puts '-' * 60 puts "Now let's be a good little #{algorithm} Al Gore Rhythm!" puts puts Rubymas.new(algorithm) end -
bil-bas revised this gist
Jan 4, 2013 . 1 changed file with 26 additions and 24 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -10,18 +10,18 @@ def to_word end GIFTS = { 1 => 'a garbage collector with an embarrassing 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) @@ -32,7 +32,7 @@ def gift(day) 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 @@ -42,7 +42,7 @@ def gift(day) 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}" @@ -52,29 +52,29 @@ def list_of_gifts_recursive(day) 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. # Lamdas are cool too. They are so cute and cuddly and fluffy. separator = lambda do |i| case i when 3..12 then ', ' when 2 then ' and ' when 1 then '' end end # 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)) << separator[current_day] end end @@ -84,9 +84,11 @@ def chorus(day, algorithm) puts end # Use three different algorithms to generate identical output. This is the way # they do it in the space shuttle, you know. Ironically, it is redundancy like # this that keeps people in work! [:recursive, :functionalish, :functional].each do |algorithm| puts '-' * 60 puts "Now let's be a good little #{algorithm} Al Gore Rhythm!" puts (1..12).each {|day| chorus day, algorithm } -
bil-bas revised this gist
Jan 4, 2013 . 1 changed file with 12 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -62,15 +62,19 @@ def list_of_gifts_functionalish(day) def list_of_gifts_functional(day) # Put the gifts in a big pile, ready for the courier service. # Lamdas are cool too. They are so cute and cuddly and fluffy. separator = lambda do |i| case i when 3..12 then ", " when 2 then " and " when 1 then "" end end # 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) << separator[current_day] end end @@ -82,7 +86,7 @@ def chorus(day, algorithm) # Use three different algorithms to generate the same output. [:recursive, :functionalish, :functional].each do |algorithm| puts "=" * 60 puts "Now let's be a good little #{algorithm} Al Gore Rhythm!" puts (1..12).each {|day| chorus day, algorithm } -
There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,89 @@ 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