Skip to content

Instantly share code, notes, and snippets.

@madrobby
Created July 6, 2009 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save madrobby/141374 to your computer and use it in GitHub Desktop.
Save madrobby/141374 to your computer and use it in GitHub Desktop.
attwenger = [
{ :year => 1991, :tracks => 18, :title => "Most" },
{ :year => 1992, :tracks => 10, :title => "Pflug" },
{ :year => 1993, :tracks => 17, :title => "Luft" },
{ :year => 1997, :tracks => 5, :title => "Song" },
{ :year => 2002, :tracks => 15, :title => "Sun" },
{ :year => 2005, :tracks => 14, :title => "dog" },
{ :year => 2006, :tracks => 17, :title => "dog2 remixes" },
{ :year => 2007, :tracks => 8, :title => "die Kia" }
]
# calculate the total amount of tracks in all albums
total = attwenger.inject(0) { |memo, album| memo + album[:tracks] }
puts "There are #{total} Attwenger tracks on #{attwenger.size} albums."
# average number of tracks
puts "The average number of tracks per album is #{total/attwenger.size}."
# which letters are used in album titles?
title_strings = attwenger.map{ |album| album[:title] }.join.gsub(/\s/,'').upcase
title_letters = title_strings.split(//).uniq.sort.join
puts "These letters are used in album titles: #{title_letters}"
# most common letters in album titles
title_count = title_letters.split(//).sort_by{|letter| title_strings.count(letter) }.
reverse.map{ |letter| "#{letter}: #{title_strings.count(letter)}" }
puts "Number of times individual letters are used in album titles, highest first:"
puts "#{title_count.join(', ')}"
class String
def count_letters
cleaned = self.gsub(/\s/,'').upcase
letters = cleaned.split(//).uniq.sort.join
letters.split(//).sort_by{|letter| cleaned.count(letter) }.
reverse.map{ |letter| "#{letter}: #{cleaned.count(letter)}" }
end
end
puts "Lorem ipsum dolor sit amet, consectetur adipisicing elit.".count_letters.join(', ')
class String
def method_missing(method)
puts "intercepting call to #{method}"
end
end
# outputs "intercepting call to krawutzikaputzi"
"Lorem ipsum dolor".krawutzikaputzi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment