Константин обнови решението на 31.10.2011 16:45 (преди около 13 години)
+class Song
+
+attr_accessor :name, :artist, :genre, :subgenre, :tags
+
+def initialize(name, artist, genre, subgenre, tags)
+ @name, @artist, @genre = name, artist, genre
+ @subgenre, @tags = subgenre, tags
+end
+
+def initialize song_as_string
+ song_array = song_as_string.strip.split(".")
+ @name = song_array[0]
+ @artist = song_array[1]
+ @genre = song_array[2].split(",")[0]
+ @subgenre = song_array[2].split(",")[1]
+ unless song_array[3] == nil
+ @tags = song_array[3].split(",")
+ end
+ proper_format
+end
+
+def proper_format
+ unless @subgenre == nil
+ @subgenre.strip!
+ end
+ unless @tags == nil
+ @tags.each do |tag|
+ tag.strip!
+ tag.downcase!
+ end
+ end
+end
+
+def to_string
+ song = @name + '.' + @artist + '.' + @genre
+ unless @subgenre == nil
+ song += (". " + @subgenre)
+ end
+ unless @tags == nil
+ song += (", " + @tags.join(", "))
+ end
+ song
+end
+
+def has_tag? tag
+ if @tags == nil
+ return tag.end_with? '!'
+ end
+ if tag.end_with? '!'
+ return (not (@tags.include? tag.chop.downcase))
+ end
+ @tags.include? tag.downcase
+end
+
+def has_tags? tags
+ if tags.kind_of?(Array) == true
+ tags.each do |tag|
+ if (has_tag? tag) == false
+ return false
+ end
+ end
+ else
+ return has_tag?(tags)
+ end
+ true
+end
+
+def single_criteria?(key, value = nil)
+ result = case key
+ when :name then (@name == value)
+ when :artist then (@artist == value)
+ when :tags then (self.has_tags? value)
+ when :filter then value.call(self)
+ end
+ result
+end
+
+def song_fits?(criteria)
+ criteria.each_pair do |key, value|
+ if single_criteria?(key, value) == false
+ return false
+ end
+ end
+ true
+end
+
+end
+
+class Collection
+
+attr_accessor :songs_as_string, :artist_tags
+
+def initialize(songs_as_string, artist_tags)
+ @songs_as_string, @artist_tags = songs_as_string, artist_tags
+end
+
+def songs_array
+ result = []
+ @songs_as_string.each_line do |line|
+ result.push(Song.new(line.squeeze(" ").gsub(". ", ".")))
+ end
+ result
+end
+
+def find(criteria)
+ songs_array.select { |song| song.song_fits?(criteria) == true}
+end
+
+end