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

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

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

Резултати

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

Код

module Criteria
def Criteria.name(song)
Criterion.new(song)
end
def Criteria.artist(artist_name)
Criterion.new(artist_name)
end
def Criteria.album(album_name)
Criterion.new(album_name)
end
end
class Criterion
include Criteria
@criterion = []
def initialize(criterion)
@criterion = [criterion]
end
def get_criterion
@criterion
end
def &(second_criterion)
@criterion << second_criterion.get_criterion()
@criterion.flatten!(1)
self
end
def |(second_criterion)
@criterion << second_criterion.get_criterion()
self
end
end
class Collection
include Enumerable
include Criteria
def initialize(collection_string)
@collection = []
records = collection_string.split("\n")
records.delete_if { |x| x == "" }
records.each_slice(3) { |slice| @collection << slice }
end
def Collection.parse(collection_string)
self.new(collection_string)
end
def artists
artists_list = @collection.collect { |record| record[1] }
artists_list.uniq
end
def albums
albums_list = @collection.collect { |record| record[2] }
albums_list.uniq
end
def names
songs_list = @collection.map { |record| record[0] }
songs_list.uniq
end
def each
@collection.each { |record| yield record }
end
def filter(criterion)
end
end

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

...FFF.FFFF

Failures:

  1) Collection can be filtered by song name
     Failure/Error: filtered.map(&:artist).should =~ ['Eva Cassidy', 'Sting']
     NoMethodError:
       undefined method `map' for nil:NilClass
     # /tmp/d20130203-23049-nla2ma/spec.rb:45: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 can be filtered by song name
     Failure/Error: filtered.map(&:album).should =~ ['The Soul Cages', "Ten Summoner's Tales"]
     NoMethodError:
       undefined method `map' for nil:NilClass
     # /tmp/d20130203-23049-nla2ma/spec.rb:50: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 can be filtered by album
     Failure/Error: filtered.map(&:name).should =~ ['Fields of Gold', 'Autumn Leaves']
     NoMethodError:
       undefined method `map' for nil:NilClass
     # /tmp/d20130203-23049-nla2ma/spec.rb:55: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 supports a conjuction of filters
     Failure/Error: filtered.map(&:album).should eq ["Ten Summoner's Tales"]
     NoMethodError:
       undefined method `map' for nil:NilClass
     # /tmp/d20130203-23049-nla2ma/spec.rb:65: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)>'

  5) Collection supports a disjunction of filters
     Failure/Error: filtered.map(&:album).should =~ [
     NoMethodError:
       undefined method `map' for nil:NilClass
     # /tmp/d20130203-23049-nla2ma/spec.rb:70: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)>'

  6) Collection supports negation of filters
     Failure/Error: filtered = collection.filter Criteria.artist('Sting') & !Criteria.name('Fields of Gold')
     NoMethodError:
       undefined method `get_criterion' for false:FalseClass
     # /tmp/d20130203-23049-nla2ma/solution.rb:28:in `&'
     # /tmp/d20130203-23049-nla2ma/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)>'

  7) Collection can be adjoined with another collection
     Failure/Error: adjoined = sting.adjoin(eva)
     NoMethodError:
       undefined method `adjoin' for nil:NilClass
     # /tmp/d20130203-23049-nla2ma/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.01029 seconds
11 examples, 7 failures

Failed examples:

rspec /tmp/d20130203-23049-nla2ma/spec.rb:43 # Collection can be filtered by song name
rspec /tmp/d20130203-23049-nla2ma/spec.rb:48 # Collection can be filtered by song name
rspec /tmp/d20130203-23049-nla2ma/spec.rb:53 # Collection can be filtered by album
rspec /tmp/d20130203-23049-nla2ma/spec.rb:63 # Collection supports a conjuction of filters
rspec /tmp/d20130203-23049-nla2ma/spec.rb:68 # Collection supports a disjunction of filters
rspec /tmp/d20130203-23049-nla2ma/spec.rb:77 # Collection supports negation of filters
rspec /tmp/d20130203-23049-nla2ma/spec.rb:82 # Collection can be adjoined with another collection

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

Мирослав обнови решението на 31.10.2012 16:47 (преди над 11 години)

+module Criteria
+ def Criteria.name(song)
+ Criterion.new(song)
+ end
+
+ def Criteria.artist(artist_name)
+ Criterion.new(artist_name)
+ end
+
+ def Criteria.album(album_name)
+ Criterion.new(album_name)
+ end
+end
+
+class Criterion
+ include Criteria
+
+ @criterion = []
+ def initialize(criterion)
+ @criterion = [criterion]
+ end
+
+ def get_criterion
+ @criterion
+ end
+
+ def &(second_criterion)
+ @criterion << second_criterion.get_criterion()
+ @criterion.flatten!(1)
+ self
+ end
+
+ def |(second_criterion)
+ @criterion << second_criterion.get_criterion()
+ self
+ end
+end
+
+class Collection
+ include Enumerable
+ include Criteria
+
+ def initialize(collection_string)
+ @collection = []
+ records = collection_string.split("\n")
+ records.delete_if { |x| x == "" }
+ records.each_slice(3) { |slice| @collection << slice }
+ end
+
+ def Collection.parse(collection_string)
+ self.new(collection_string)
+ end
+
+ def artists
+ artists_list = @collection.collect { |record| record[1] }
+ artists_list.uniq
+ end
+
+ def albums
+ albums_list = @collection.collect { |record| record[2] }
+ albums_list.uniq
+ end
+
+ def names
+ songs_list = @collection.map { |record| record[0] }
+ songs_list.uniq
+ end
+
+ def each
+ @collection.each { |record| yield record }
+ end
+
+ def filter(criterion)
+ end
+end