Решение на Втора задача от Христо Хърсев

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

Към профила на Христо Хърсев

Резултати

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

Код

#не е като да е.. решение
class Song
attr_accessor :name, :artist, :album
def initialize (song_string)
song_parts = song_string.split("\n")
@name = song_parts[0]
@artist = song_parts[1]
@album = song_parts[2]
end
end
class Collection
include Enumerable
def each
@songs.each do |song|
yield song
end
end
def initialize(text)
@songs = []
text.split("\n\n").each do |song|
@songs << Song.new(song)
end
end
def self.parse(text)
self.new text
end
def artists
map { |song| song.artist }.uniq
end
def albums
map { |song| song.album }.uniq
end
def names
map { |song| song.name }.uniq
end
#returning Array.. not collection *fix*
def filter criterion
@songs.select { |i| criterion.match? i }
end
#filtered_collection... not an Array
def adjoin filtered_collection
#concat but for Collection
end
end
class Criteria
attr_accessor :name, :artist, :album
def self.name text
result = Criteria.new
result.name = text
result
end
def self.artist text
result = Criteria.new
result.artist = text
result
end
def self.album text
result = Criteria.new
result.album = text
result
end
def match? song
song.name == @name or song.artist == @artist or song.album == @album
end
def &
end
def !
end
def |
end
end

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

.......FFFF

Failures:

  1) Collection supports a conjuction of filters
     Failure/Error: filtered = collection.filter Criteria.artist('Sting') & Criteria.name('Fields of Gold')
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20130203-23049-gr8prw/solution.rb:79:in `&'
     # /tmp/d20130203-23049-gr8prw/spec.rb:64:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) Collection supports a disjunction of filters
     Failure/Error: filtered = collection.filter Criteria.artist('Sting') | Criteria.name('Fields of Gold')
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20130203-23049-gr8prw/solution.rb:87:in `|'
     # /tmp/d20130203-23049-gr8prw/spec.rb:69:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) Collection supports negation of filters
     Failure/Error: filtered = collection.filter Criteria.artist('Sting') & !Criteria.name('Fields of Gold')
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20130203-23049-gr8prw/solution.rb:79:in `&'
     # /tmp/d20130203-23049-gr8prw/spec.rb:78:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) Collection can be adjoined with another collection
     Failure/Error: adjoined = sting.adjoin(eva)
     NoMethodError:
       undefined method `adjoin' for #<Array:0x93c52b8>
     # /tmp/d20130203-23049-gr8prw/spec.rb:85:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.01008 seconds
11 examples, 4 failures

Failed examples:

rspec /tmp/d20130203-23049-gr8prw/spec.rb:63 # Collection supports a conjuction of filters
rspec /tmp/d20130203-23049-gr8prw/spec.rb:68 # Collection supports a disjunction of filters
rspec /tmp/d20130203-23049-gr8prw/spec.rb:77 # Collection supports negation of filters
rspec /tmp/d20130203-23049-gr8prw/spec.rb:82 # Collection can be adjoined with another collection

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

Христо обнови решението на 31.10.2012 15:22 (преди над 11 години)

+#не е като да е.. решение
+class Song
+ attr_accessor :name, :artist, :album
+ def initialize (song_string)
+ song_parts = song_string.split("\n")
+ @name = song_parts[0]
+ @artist = song_parts[1]
+ @album = song_parts[2]
+ end
+end
+
+class Collection
+ include Enumerable
+
+ def each
+ @songs.each do |song|
+ yield song
+ end
+ end
+
+ def initialize(text)
+ @songs = []
+ text.split("\n\n").each do |song|
+ @songs << Song.new(song)
+ end
+ end
+
+ def self.parse(text)
+ self.new text
+ end
+
+ def artists
+ map { |song| song.artist }.uniq
+ end
+
+ def albums
+ map { |song| song.album }.uniq
+ end
+
+ def names
+ map { |song| song.name }.uniq
+ end
+
+#returning Array.. not collection *fix*
+ def filter criterion
+ @songs.select { |i| criterion.match? i }
+ end
+
+#filtered_collection... not an Array
+ def adjoin filtered_collection
+ #concat but for Collection
+ end
+end
+
+class Criteria
+ attr_accessor :name, :artist, :album
+ def self.name text
+ result = Criteria.new
+ result.name = text
+ result
+ end
+
+ def self.artist text
+ result = Criteria.new
+ result.artist = text
+ result
+ end
+
+ def self.album text
+ result = Criteria.new
+ result.album = text
+ result
+ end
+
+ def match? song
+ song.name == @name or song.artist == @artist or song.album == @album
+ end
+
+ def &
+
+ end
+
+ def !
+
+ end
+
+ def |
+
+ end
+end
  • Не оставяй интервали между името на метода и скобите около параметрите, напр. на ред 4 трябва да е def initialize(...)
  • Редове 5 до 8 могат да се запишат така: @name, @artist, @album = song_stirng.split("\n")
  • Collection#each може да стане като само вземеш блок и го подадеш без промяна на @songs.each: def each(&block); @songs.each(&block); end
  • Collection#initialize може да се напише с map
  • В Collection.parse, self вече е Collection и се подразбира, т.е. се и изпуска; напр. Collection.new, self.new и new е едно и също в този контекст
  • Струва ми се, че идентацията ти на места е счупена (напр. в метода Collection#filter)
  • i е неподходящо име на променлива на ред 46
  • В клас-методите на Criteria имаш повторение на код, което вероятно може да се избегне; виждам, че класът ти е in progress, което значи, че може би ще го промениш :)