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

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

Към профила на Илиян Величков

Резултати

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

Код

class Collection
include Enumerable
def initialize(songs_array)
@songs = songs_array
end
def self.parse(text)
songs = []
text.split("\n\n").map do |song_data|
songs << Song.new(*song_data.split("\n"))
end
Collection.new(songs)
end
def artists
songs.map { |song| song.artist }.uniq
end
def albums
songs.map { |song| song.album }.uniq
end
def names
songs.map { |song| song.name }.uniq
end
def filter(criteria)
Collection.new(songs.select { |song| criteria.fulfil? song } )
end
def adjoin(other)
Collection.new(songs | other.songs)
end
def each(&block)
songs.each(&block)
end
protected
attr_reader :songs
end
class Song
attr_reader :name, :artist, :album
def initialize(name, artist, album)
@name = name
@artist = artist
@album = album
end
end
module Criteria
def self.name(text)
Criterion.new(->(song) { song.name == text } )
end
def self.artist(text)
Criterion.new(->(song) { song.artist == text } )
end
def self.album(text)
Criterion.new(->(song) { song.album == text } )
end
class Criterion
attr_reader :fulfil_criterion
def initialize(given_criterion)
@fulfil_criterion = given_criterion
end
def fulfil?(song)
fulfil_criterion.(song)
end
def &(other)
Criterion.new(->(song) { fulfil?(song) && other.fulfil?(song) } )
end
def |(other)
Criterion.new(->(song) { fulfil?(song) || other.fulfil?(song) } )
end
def !
Criterion.new(->(song) { !fulfil? song } )
end
end
end

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

...........

Finished in 0.00994 seconds
11 examples, 0 failures

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

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

+class Collection
+ include Enumerable
+
+ def initialize(songs_array)
+ @songs = songs_array
+ end
+
+ def self.parse(text)
+ songs = []
+ text.split("\n\n").collect do |song_data|
+ songs << Song.new(*song_data.split("\n"))
+ end
+ Collection.new songs
+ end
+
+ def artists
+ songs.collect { |song| song.artist }.uniq
+ end
+
+ def albums
+ songs.collect { |song| song.album }.uniq
+ end
+
+ def names
+ songs.collect { |song| song.name }.uniq
+ end
+
+ def filter(criteria)
+ Collection.new songs.select { |song| criteria.fulfil? song }
+ end
+
+ def adjoin(other)
+ Collection.new songs | other.songs
+ end
+
+ def each(&block)
+ songs.each &block
+ end
+
+ protected
+ attr_reader :songs
+end
+
+class Song
+ attr_reader :name, :artist, :album
+
+ def initialize name, artist, album
+ @name = name
+ @artist = artist
+ @album = album
+ end
+end
+
+module Criteria
+ def self.name(text)
+ Criterion.new(lambda { |song| song.name == text })
+ end
+
+ def self.artist(text)
+ Criterion.new(lambda { |song| song.artist == text })
+ end
+
+ def self.album(text)
+ Criterion.new(lambda { |song| song.album == text })
+ end
+
+ class Criterion
+ attr_reader :fulfil_criterion
+
+ def initialize(given_criterion)
+ @fulfil_criterion = given_criterion
+ end
+
+ def fulfil?(song)
+ fulfil_criterion.call(song)
+ end
+
+ def &(other)
+ Criterion.new lambda { |song| fulfil? song and other.fulfil? song }
+ end
+
+ def |(other)
+ Criterion.new lambda { |song| fulfil? song or other.fulfil? song }
+ end
+
+ def !
+ Criterion.new lambda { |song| !fulfil? song }
+ end
+ end
+end

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

class Collection
include Enumerable
def initialize(songs_array)
@songs = songs_array
end
def self.parse(text)
songs = []
text.split("\n\n").collect do |song_data|
songs << Song.new(*song_data.split("\n"))
end
Collection.new songs
end
def artists
songs.collect { |song| song.artist }.uniq
end
def albums
songs.collect { |song| song.album }.uniq
end
def names
songs.collect { |song| song.name }.uniq
end
def filter(criteria)
Collection.new songs.select { |song| criteria.fulfil? song }
end
def adjoin(other)
Collection.new songs | other.songs
end
def each(&block)
songs.each &block
end
protected
attr_reader :songs
end
class Song
attr_reader :name, :artist, :album
def initialize name, artist, album
@name = name
@artist = artist
@album = album
end
end
module Criteria
def self.name(text)
- Criterion.new(lambda { |song| song.name == text })
+ Criterion.new lambda { |song| song.name == text }
end
def self.artist(text)
- Criterion.new(lambda { |song| song.artist == text })
+ Criterion.new lambda { |song| song.artist == text }
end
def self.album(text)
- Criterion.new(lambda { |song| song.album == text })
+ Criterion.new lambda { |song| song.album == text }
end
class Criterion
attr_reader :fulfil_criterion
def initialize(given_criterion)
@fulfil_criterion = given_criterion
end
def fulfil?(song)
fulfil_criterion.call(song)
end
def &(other)
Criterion.new lambda { |song| fulfil? song and other.fulfil? song }
end
def |(other)
Criterion.new lambda { |song| fulfil? song or other.fulfil? song }
end
def !
Criterion.new lambda { |song| !fulfil? song }
end
end
end

Илиян обнови решението на 31.10.2012 15:02 (преди около 12 години)

class Collection
include Enumerable
def initialize(songs_array)
@songs = songs_array
end
def self.parse(text)
songs = []
- text.split("\n\n").collect do |song_data|
+ text.split("\n\n").map do |song_data|
songs << Song.new(*song_data.split("\n"))
end
- Collection.new songs
+ Collection.new(songs)
end
def artists
- songs.collect { |song| song.artist }.uniq
+ songs.map { |song| song.artist }.uniq
end
def albums
- songs.collect { |song| song.album }.uniq
+ songs.map { |song| song.album }.uniq
end
def names
- songs.collect { |song| song.name }.uniq
+ songs.map { |song| song.name }.uniq
end
def filter(criteria)
- Collection.new songs.select { |song| criteria.fulfil? song }
+ Collection.new(songs.select { |song| criteria.fulfil? song } )
end
def adjoin(other)
- Collection.new songs | other.songs
+ Collection.new(songs | other.songs)
end
def each(&block)
- songs.each &block
+ songs.each(&block)
end
protected
attr_reader :songs
end
class Song
attr_reader :name, :artist, :album
- def initialize name, artist, album
+ def initialize(name, artist, album)
@name = name
@artist = artist
@album = album
end
end
module Criteria
def self.name(text)
- Criterion.new lambda { |song| song.name == text }
+ Criterion.new(->(song) { song.name == text } )
end
def self.artist(text)
- Criterion.new lambda { |song| song.artist == text }
+ Criterion.new(->(song) { song.artist == text } )
end
def self.album(text)
- Criterion.new lambda { |song| song.album == text }
+ Criterion.new(->(song) { song.album == text } )
end
class Criterion
attr_reader :fulfil_criterion
def initialize(given_criterion)
@fulfil_criterion = given_criterion
end
def fulfil?(song)
- fulfil_criterion.call(song)
+ fulfil_criterion.(song)
end
def &(other)
- Criterion.new lambda { |song| fulfil? song and other.fulfil? song }
+ Criterion.new(->(song) { fulfil?(song) && other.fulfil?(song) } )
end
def |(other)
- Criterion.new lambda { |song| fulfil? song or other.fulfil? song }
+ Criterion.new(->(song) { fulfil?(song) || other.fulfil?(song) } )
end
def !
- Criterion.new lambda { |song| !fulfil? song }
+ Criterion.new(->(song) { !fulfil? song } )
end
end
end

Ориентирах се по style guide-a за скобите на .new когато е с аргументи, затова има толкова много скоби. На някои места в лекциите(примерно тук http://fmi.ruby.bg/lectures/03-intro-to-oo-ruby#5 и тук http://fmi.ruby.bg/lectures/03-intro-to-oo-ruby#7) са пропуснати. В Criterion#& и Criterion#|, така написано, аргументите на метода fulfil? не могат да се без скоби. Може да има и по-готини стилистични грешки, които да съм допуснал. :)