Неделчо обнови решението на 31.10.2012 01:11 (преди над 12 години)
+text = "Fields of Gold
+Sting
+Ten Summoner\'s Tales
+
+Mad About You
+Sting
+The Soul Cages
+
+Fields of Gold
+Eva Cassidy
+Live at Blues Alley
+
+Autumn Leaves
+Eva Cassidy
+Live at Blues Alley
+
+Autumn Leaves
+Bill Evans
+Portrait in Jazz
+
+Brain of J.F.K
+Pearl Jam
+Yield
+
+Jeremy
+Pearl Jam
+Ten
+
+Come Away With Me
+Norah Johnes
+One
+
+Acknowledgment
+John Coltrane
+A Love Supreme
+
+Ruby, My Dear
+Thelonious Monk
+Mysterioso"
+
+class Song
+ def initialize(name , artist, album)
+ @name, @artist, @album = name, artist, album
+ end
+
+ def name
+ "#{@name}"
+ end
+
+ def artist
+ "#{@artist}"
+ end
+
+ def album
+ "#{@album}"
+ end
+end
+
+class Collection
+ include Enumerable
+
+ def initialize(songs)
+ @songs = songs
+ end
+
+ def Collection.parse(text)
+ Collection.new text.split("\n").each_slice(4).map{|r,s,a,e| Song.new(r,s,a)}
+ end
+
+ def names
+ nams = []
+ @songs.map{ |s| nams.push s.name}
+ nams.uniq
+ end
+
+ def artists
+ arts = []
+ @songs.map{ |s| arts.push s.artist }
+ arts.uniq
+ end
+
+ def albums
+ albs = []
+ @songs.map{ |s| albs.push s.album }
+ albs .uniq
+ end
+
+ def each
+ @songs.select{ |s| yield s }
+ end
+
+ def filter(criteria)
+ value = criteria.get_criteria
+ @songs.select{|s| s.name == value[1] and value[0] =="name"}
+ @songs.select{|s| s.artist == value[1] and value[0] =="artist"}
+ @songs.select{|s| s.album == value[1]and value[0] =="album"}
+ end
+end
+
+class Criteria
+ def initialize( field, value)
+ @field, @value = field, value
+ end
+
+ def Criteria.name(value)
+ Criteria.new "name", value
+ end
+
+ def Criteria.artist(value)
+ Criteria.new "artist", value
+ end
+
+ def Criteria.album(value)
+ Criteria.new "album", value
+ end
+
+ def get_criteria
+ [ @field, @value ]
+ end
+end