Решение на Втора задача от Милан Миланов

Обратно към всички решения

Към профила на Милан Миланов

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 11 успешни тест(а)
  • 0 неуспешни тест(а)

Код

class Collection
include Enumerable
def initialize(songs)
@songs = songs
end
def self.parse(text)
@songs = []
text.lines.map(&:chomp).each_slice(4) do |lines|
@songs << Song.new(lines[0], lines[1], lines[2])
end
Collection.new(@songs)
end
def names
@songs.uniq { |song| song.name }.map { |song| song.name }
end
def artists
@songs.uniq { |song| song.artist }.map { |song| song.artist }
end
def albums
@songs.uniq { |song| song.album }.map { |song| song.album }
end
def filter(criteria)
filtered_songs = @songs.select do |song|
criteria.criterion.call(song)
end
Collection.new(filtered_songs)
end
def adjoin(collection)
Collection.new(@songs + collection.songs)
end
def each(&block)
@songs.each(&block)
end
attr_reader :songs
end
class Criteria
def initialize(criteria)
@criterion = criteria
end
def self.name(name)
Criteria.new(->song { song.name == name } )
end
def self.artist(artist)
Criteria.new(->song { song.artist == artist } )
end
def self.album(album)
Criteria.new(->song { song.album == album } )
end
def !
Criteria.new(->song { !criterion.call(song) })
end
def |(other)
Criteria.new(->song { criterion.call(song) || other.criterion.call(song) })
end
def &(other)
Criteria.new(->song { criterion.call(song) && other.criterion.call(song) })
end
attr_reader :criterion
end
class Song
def initialize(name, artist, album)
@name = name
@artist = artist
@album = album
end
attr_reader :name, :artist, :album
end
class ReadMusicFile
def initialize(music_file)
@text_from_file = File.exists?(music_file) ? File.read(music_file) : ''
end
attr_reader :text_from_file
end

Лог от изпълнението

...........

Finished in 0.01029 seconds
11 examples, 0 failures

История (3 версии и 6 коментара)

Милан обнови решението на 31.10.2012 00:20 (преди около 12 години)

+class Collection
+ include Enumerable
+
+ def initialize(songs)
+ @songs = songs
+ end
+
+ def self.parse(text)
+ text.lines.map(&:chomp).each_slice(4) do |lines|
+ @@songs ||= []
+ @@songs << Song.new(lines[0], lines[1], lines[2])
+ end
+ Collection.new(@@songs)
+ end
+
+ def names
+ @songs.uniq { |song| song.name }.map { |song| song.name }
+ end
+
+ def artists
+ @songs.uniq { |song| song.artist }.map { |song| song.artist }
+ end
+
+ def albums
+ @songs.uniq { |song| song.album }.map { |song| song.album }
+ end
+
+ def filter(criteria)
+ @filtered_songs = @songs.select do |song|
+ criteria.criterion.call(song)
+ end
+ Collection.new(@filtered_songs)
+ end
+
+ def adjoin(collection)
+ Collection.new(@songs + collection.songs)
+ end
+
+ def each(&block)
+ @songs.each(&block)
+ end
+
+ attr_reader :songs
+end
+
+class Criteria
+ def initialize(criteria)
+ @criterion = criteria
+ end
+
+ def self.name(name)
+ Criteria.new(->song { song.name == name } )
+ end
+
+ def self.artist(artist)
+ Criteria.new(->song { song.artist == artist } )
+ end
+
+ def self.album(album)
+ Criteria.new(->song { song.album == album } )
+ end
+
+ def !
+ Criteria.new(->song { !criterion.call(song) })
+ end
+
+ def |(other)
+ Criteria.new(->song { criterion.call(song) || other.criterion.call(song) })
+ end
+
+ def &(other)
+ Criteria.new(->song { criterion.call(song) && other.criterion.call(song) })
+ end
+
+ attr_reader :criterion
+end
+
+class Song
+ def initialize(name, artist, album)
+ @name = name
+ @artist = artist
+ @album = album
+ end
+
+ attr_reader :name, :artist, :album
+end
+
+class ReadMusicFile
+ def initialize(music_file)
+ @text_from_file = File.exists?(music_file) ? File.read(music_file) : ''
+ end
+
+ attr_reader :text_from_file
+end

Милан обнови решението на 31.10.2012 08:48 (преди около 12 години)

class Collection
include Enumerable
def initialize(songs)
@songs = songs
end
def self.parse(text)
text.lines.map(&:chomp).each_slice(4) do |lines|
- @@songs ||= []
- @@songs << Song.new(lines[0], lines[1], lines[2])
+ @songs ||= []
+ @songs << Song.new(lines[0], lines[1], lines[2])
end
- Collection.new(@@songs)
+ Collection.new(@songs)
end
def names
@songs.uniq { |song| song.name }.map { |song| song.name }
end
def artists
@songs.uniq { |song| song.artist }.map { |song| song.artist }
end
def albums
@songs.uniq { |song| song.album }.map { |song| song.album }
end
def filter(criteria)
@filtered_songs = @songs.select do |song|
criteria.criterion.call(song)
end
Collection.new(@filtered_songs)
end
def adjoin(collection)
Collection.new(@songs + collection.songs)
end
def each(&block)
@songs.each(&block)
end
attr_reader :songs
end
class Criteria
def initialize(criteria)
@criterion = criteria
end
def self.name(name)
Criteria.new(->song { song.name == name } )
end
def self.artist(artist)
Criteria.new(->song { song.artist == artist } )
end
def self.album(album)
Criteria.new(->song { song.album == album } )
end
def !
Criteria.new(->song { !criterion.call(song) })
end
def |(other)
Criteria.new(->song { criterion.call(song) || other.criterion.call(song) })
end
def &(other)
Criteria.new(->song { criterion.call(song) && other.criterion.call(song) })
end
attr_reader :criterion
end
class Song
def initialize(name, artist, album)
@name = name
@artist = artist
@album = album
end
attr_reader :name, :artist, :album
end
class ReadMusicFile
def initialize(music_file)
@text_from_file = File.exists?(music_file) ? File.read(music_file) : ''
end
attr_reader :text_from_file
end

Защото не ползваш @filtered_songs никъде другаде. Ако е локална променлива, областта й на видимост е извикването на функцията - конструираш нови песни, подаваш ги на Collection.new и толкова - вече не ти трябва.

Ако я направиш instance обаче, продължава да живее след извикването на метода. Не я ползваш никъде, обаче. Съответно, кодът ти е объркващ - от една страна казва "това е instance variable и ще се ползва по-натам", от друга - не се.

Милан обнови решението на 31.10.2012 12:38 (преди около 12 години)

class Collection
include Enumerable
def initialize(songs)
@songs = songs
end
def self.parse(text)
+ @songs = []
text.lines.map(&:chomp).each_slice(4) do |lines|
- @songs ||= []
@songs << Song.new(lines[0], lines[1], lines[2])
end
Collection.new(@songs)
end
def names
@songs.uniq { |song| song.name }.map { |song| song.name }
end
def artists
@songs.uniq { |song| song.artist }.map { |song| song.artist }
end
def albums
@songs.uniq { |song| song.album }.map { |song| song.album }
end
def filter(criteria)
- @filtered_songs = @songs.select do |song|
+ filtered_songs = @songs.select do |song|
criteria.criterion.call(song)
end
- Collection.new(@filtered_songs)
+ Collection.new(filtered_songs)
end
def adjoin(collection)
Collection.new(@songs + collection.songs)
end
def each(&block)
@songs.each(&block)
end
attr_reader :songs
end
class Criteria
def initialize(criteria)
@criterion = criteria
end
def self.name(name)
Criteria.new(->song { song.name == name } )
end
def self.artist(artist)
Criteria.new(->song { song.artist == artist } )
end
def self.album(album)
Criteria.new(->song { song.album == album } )
end
def !
Criteria.new(->song { !criterion.call(song) })
end
def |(other)
Criteria.new(->song { criterion.call(song) || other.criterion.call(song) })
end
def &(other)
Criteria.new(->song { criterion.call(song) && other.criterion.call(song) })
end
attr_reader :criterion
end
class Song
def initialize(name, artist, album)
@name = name
@artist = artist
@album = album
end
attr_reader :name, :artist, :album
end
class ReadMusicFile
def initialize(music_file)
@text_from_file = File.exists?(music_file) ? File.read(music_file) : ''
end
attr_reader :text_from_file
end