public
Last active — forked from madrobby/gist:141374

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.