Елена обнови решението на 26.10.2012 07:57 (преди около 12 години)
+class Song
+ attr_accessor :name, :artist, :album
+
+ def initialize(name, artist, album)
+ @name, @artist, @album = name, artist, album
+ end
+end
+
+class Collection
+ attr_accessor :songs
+
+ include Enumerable
+
+ def initialize songs
+ @songs = songs
+ end
+
+ def Collection.parse(text)
+ songs = []
+ text.split("\n").each_slice(4) {|x| songs << Song.new(x[0], x[1], x[2])}
+ Collection.new songs
+ end
+
+ def artists
+ artistsArray = songs.map {|x| x.artist}.uniq
+ end
+
+ def albums
+ albumsArray = songs.map {|x| x.album}.uniq
+ end
+
+ def names
+ namesArray = songs.map {|x| x.name}.uniq
+ end
+
+ def each
+ @songs.each {|item| yield item}
+ end
+
+ def adjoin collection
+ return Collection.new (@songs + collection.songs).uniq
+ end
+
+ def filter criteria
+ Collection.new songs.select{|item| criteria.desired?(item)}
+ end
+end
+
+class Criteria
+ attr_accessor :filters
+
+ def initialize filters
+ @filters = filters
+ end
+
+ def Criteria.name name
+ Criteria.new([{"name" => name}])
+ end
+
+ def Criteria.artist artist
+ Criteria.new([{"artist" => artist}])
+ end
+
+ def Criteria.album album
+ Criteria.new([{"album" => album}])
+ end
+
+ def & other
+ Criteria.new(["&"] + @filters + other.filters)
+ end
+
+ def | other
+ Criteria.new(["|"] + @filters + other.filters)
+ end
+
+ def !
+ Criteria.new(['!'] + @filters)
+ end
+
+ def desired? song
+ case @filters.length
+ when 1 then return match? song
+ when 2 then return !Criteria.new([@filters[1]]).desired?(song)
+ else filterWithTwoConditions song
+ end
+ end
+
+ def filterWithTwoConditions song
+ condition1 = Criteria.new([@filters[1]]).desired?(song)
+ condition2 = Criteria.new([@filters[2]]).desired?(song)
+ return true if @filters[0] == "&" && condition1 && condition2
+ return @filters[0] == "|" && (condition1 || condition2)
+ end
+
+ private
+ def match? song
+ return true if @filters[0]["name"] == song.name
+ return true if @filters[0]["artist"] == song.artist
+ return true if @filters[0]["album"] == song.album
+ false
+ end
+end
Идентацията ти е объркана. Имаш излишни return
-и. Не спазваш конвенцията. Вероятно не си пуснала и примерния тест.
Моля, погледни си решението.