Свилен обнови решението на 30.10.2012 17:33 (преди около 12 години)
+lass Song
+
+ attr_accessor :name, :artist, :album
+ def initialize(name, artist, album)
+ @name, @artist, @album = name, artist, album
+ end
+
+ def same_name?(name)
+ @name == name
+ end
+
+ def same_artist?(artist)
+ @artist == artist
+ end
+
+ def same_album?(album)
+ @album == album
+ end
+
+ def same_song?(song)
+ self == song
+ end
+
+end
+
+class Collection
+ attr_accessor :all
+ def initialize(songs=0)
+ @all = songs
+ end
+
+ def Collection.parse(patch)
+ songs = []
+ patch.each_line.each_slice(4) do |song|
+ songs<<Song.new(song[0].chomp,song[1].chomp, song[2].chomp)
+ end
+ Collection.new(songs)
+ end
+
+ def names
+ result = @all.map { |song| song.name }
+ result.uniq
+ end
+
+ def artists
+ result = @all.map { |song| song.artist }
+ result.uniq
+ end
+
+ def albums
+ result = @all.map { |arr| arr.album }
+ result.uniq
+ end
+
+ def filter(arg)
+ songs = @all.select{ |song| arg.result.call song }
+ Collection.new(songs)
+ end
+
+ def adjoin(arg)
+ Collection.new( @all | arg.all )
+ end
+
+end
+
+class Criteria
+ attr_accessor :result
+ def initialize(result)
+ # p result
+ @result = result
+ end
+
+ def &(arg)
+ Criteria.new(lambda {|song| (@result.call(song) && arg.result.call(song))})
+ end
+
+ def |(arg)
+ Criteria.new(lambda {|song| (@result.call(song) || arg.result.call(song))})
+ end
+
+ def !
+ Criteria.new(lambda {|song| (!@result.call (song))})
+ end
+
+ def Criteria.name(name)
+ Criteria.new(lambda {|song| name == song.name})
+ end
+
+ def Criteria.album(album)
+ Criteria.new(lambda {|song| album == song.album})
+ end
+
+ def Criteria.artist(artist)
+ return Criteria.new(lambda {|song| artist == song.artist })
+ end
+
+end