Николай обнови решението на 31.10.2011 16:55 (преди около 13 години)
+class Song
+ def initialize(song_as_string, artist_tags)
+ song_array = song_as_string.split(/\s*\.\s*/)
+ @name = song_array[0]
+ @artist = song_array[1]
+ @genre = song_array[2].split(/\s*\,\s*/)[0]
+ @subgenre = song_array[2].split(/\s*\,\s*/)[1]
+ @tags = [@genre.downcase]
+ if artist_tags[@artist]
+ @tags |= artist_tags[@artist]
+ end
+ init_tags(song_array[3])
+ end
+ def init_tags(tags_string)
+ if tags_string
+ @tags |= tags_string.split(/\s*\,\s*/)
+ end
+ if @subgenre
+ @tags << @subgenre.downcase
+ end
+ end
+ def name
+ @name
+ end
+ def artist
+ @artist
+ end
+ def genre
+ @genre
+ end
+ def subgenre
+ @subgenre
+ end
+ def tags
+ @tags
+ end
+end
+
+
+class Collection
+ def initialize(songs_as_string, artist_tags)
+ @songs_as_string = songs_as_string
+ @artist_tags = artist_tags
+ @songs = []
+ songs_as_string.each_line {|s| @songs << Song.new(s.strip, artist_tags)}
+ end
+
+ def find(criteria)
+ a = @songs
+ if criteria[:tags]
+ a = find_tags(criteria[:tags], a)
+ end
+ if criteria[:artist]
+ a = find_artist(criteria[:artist], a)
+ end
+ if criteria[:name]
+ a = find_name(criteria[:name], a)
+ end
+ a
+ end
+
+ def find_tags(tags_string, array)
+ array = array.select {|v| v.tags.include?(tags_string)}
+ end
+ def find_artist(artist_string, array)
+ array.select {|v| v.artist == artist_string}
+ end
+ def find_name(name_string, array)
+ array.select {|v| v.name == name_string}
+ end
+end