Решение на Втора задача от Изабела Недялкова

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

Към профила на Изабела Недялкова

Резултати

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

Код

class Song
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
end
class Collection
include Enumerable
attr_accessor :songs
def initialize(songs)
@songs = songs
end
def each
@songs.each { |song| yield song }
end
def self.parse(text)
parsed_songs = text.split("\n").each_slice(4).map do |name, artist, album|
Song.new(name, artist, album)
end
Collection.new(parsed_songs)
end
def names
@songs.map(&:name).uniq
end
def artists
@songs.map(&:artist).uniq
end
def albums
@songs.map(&:album).uniq
end
def filter(criteria)
filtered_songs = @songs.select { |song| criteria.meets?(song) }
Collection.new(filtered_songs)
end
def adjoin(collection)
Collection.new(songs | collection.songs)
end
end
class Criteria
def initialize(&selector)
@selector = selector
end
def meets?(song)
@selector.call(song)
end
def self.name(request)
Criteria.new { |song| request == song.name }
end
def self.artist(request)
Criteria.new { |song| request == song.artist }
end
def self.album(request)
Criteria.new { |song| request == song.album }
end
def &(other)
Criteria.new { |song| meets?(song) & other.meets?(song) }
end
def |(other)
Criteria.new { |song| meets?(song) | other.meets?(song) }
end
def !
Criteria.new { |song| !meets?(song) }
end
end

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

...........

Finished in 0.0103 seconds
11 examples, 0 failures

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

Изабела обнови решението на 30.10.2012 01:03 (преди около 12 години)

+class Song
+ attr_accessor :name, :artist, :album
+
+ def initialize(name, artist, album)
+ @name, @artist, @album = name, artist, album
+ end
+end
+
+class Collection
+ include Enumerable
+
+ attr_accessor :songs
+
+ def initialize(songs)
+ @songs = songs
+ end
+
+ def each
+ @songs.each { |song| yield song }
+ end
+
+ def self.parse(text)
+ parsed_songs = text.split("\n").each_slice(4).
+ map { |name, artist, album| Song.new(name, artist, album) }
+ Collection.new(parsed_songs)
+ end
+
+ def names
+ @songs.map(&:name).uniq
+ end
+
+ def artists
+ @songs.map(&:artist).uniq
+ end
+
+ def albums
+ @songs.map(&:album).uniq
+ end
+
+ def filter(criteria)
+ filtered_songs = @songs.select { |song| criteria.selector.call(song) }
+ Collection.new(filtered_songs)
+ end
+
+ def adjoin(collection)
+ Collection.new(songs + collection.songs)
+ end
+end
+
+class Criteria
+ attr_accessor :selector
+
+ def initialize(selector)
+ @selector = selector
+ end
+
+ def self.name(request)
+ Criteria.new ->(song) { request == song.name }
+ end
+
+ def self.artist(request)
+ Criteria.new ->(song) { request == song.artist }
+ end
+
+ def self.album(request)
+ Criteria.new ->(song) { request == song.album }
+ end
+
+ def &(other)
+ Criteria.new ->(song) { selector.call(song) & other.selector.call(song) }
+ end
+
+ def |(other)
+ Criteria.new ->(song) { selector.call(song) | other.selector.call(song) }
+ end
+
+ def !
+ Criteria.new ->(song) { !selector.call(song) }
+ end
+end

Изабела обнови решението на 30.10.2012 15:18 (преди около 12 години)

class Song
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
end
class Collection
include Enumerable
attr_accessor :songs
def initialize(songs)
@songs = songs
end
def each
@songs.each { |song| yield song }
end
def self.parse(text)
parsed_songs = text.split("\n").each_slice(4).
map { |name, artist, album| Song.new(name, artist, album) }
Collection.new(parsed_songs)
end
def names
@songs.map(&:name).uniq
end
def artists
@songs.map(&:artist).uniq
end
def albums
@songs.map(&:album).uniq
end
def filter(criteria)
filtered_songs = @songs.select { |song| criteria.selector.call(song) }
Collection.new(filtered_songs)
end
def adjoin(collection)
- Collection.new(songs + collection.songs)
+ Collection.new(songs | collection.songs)
end
end
class Criteria
attr_accessor :selector
def initialize(selector)
@selector = selector
end
def self.name(request)
Criteria.new ->(song) { request == song.name }
end
def self.artist(request)
Criteria.new ->(song) { request == song.artist }
end
def self.album(request)
Criteria.new ->(song) { request == song.album }
end
def &(other)
Criteria.new ->(song) { selector.call(song) & other.selector.call(song) }
end
def |(other)
Criteria.new ->(song) { selector.call(song) | other.selector.call(song) }
end
def !
Criteria.new ->(song) { !selector.call(song) }
end
end

Много добре, но ето няколко неща.

  • Няма нужда от Criteria#selector. Вместо това, просто добави метод в критерия, който приема песен и връща булева стойност.
  • Няма нужда да подаваш ламбда, когато можеш да подадеш блок.
  • Подреждането на 23-24 ред е гадно. Ползвай do/end за map

Изабела обнови решението на 31.10.2012 13:43 (преди около 12 години)

class Song
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
end
class Collection
include Enumerable
attr_accessor :songs
def initialize(songs)
@songs = songs
end
def each
@songs.each { |song| yield song }
end
def self.parse(text)
- parsed_songs = text.split("\n").each_slice(4).
- map { |name, artist, album| Song.new(name, artist, album) }
+ parsed_songs = text.split("\n").each_slice(4).map do |name, artist, album|
+ Song.new(name, artist, album)
+ end
Collection.new(parsed_songs)
end
def names
@songs.map(&:name).uniq
end
def artists
@songs.map(&:artist).uniq
end
def albums
@songs.map(&:album).uniq
end
def filter(criteria)
- filtered_songs = @songs.select { |song| criteria.selector.call(song) }
+ filtered_songs = @songs.select { |song| criteria.meets?(song) }
Collection.new(filtered_songs)
end
def adjoin(collection)
Collection.new(songs | collection.songs)
end
end
class Criteria
- attr_accessor :selector
-
- def initialize(selector)
+ def initialize(&selector)
@selector = selector
end
+ def meets?(song)
+ @selector.call(song)
+ end
+
def self.name(request)
- Criteria.new ->(song) { request == song.name }
+ Criteria.new { |song| request == song.name }
end
def self.artist(request)
- Criteria.new ->(song) { request == song.artist }
+ Criteria.new { |song| request == song.artist }
end
def self.album(request)
- Criteria.new ->(song) { request == song.album }
+ Criteria.new { |song| request == song.album }
end
def &(other)
- Criteria.new ->(song) { selector.call(song) & other.selector.call(song) }
+ Criteria.new { |song| meets?(song) & other.meets?(song) }
end
def |(other)
- Criteria.new ->(song) { selector.call(song) | other.selector.call(song) }
+ Criteria.new { |song| meets?(song) | other.meets?(song) }
end
def !
- Criteria.new ->(song) { !selector.call(song) }
+ Criteria.new { |song| !meets?(song) }
end
end