Решение на Втора задача от Николай Шегунов

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

Към профила на Николай Шегунов

Резултати

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

Код

class Song
attr_accessor :name, :artist, :album
def initialize( name, artist, album )
@name, @artist,@album = name, artist,album
end
end
class Collection
include Enumerable
def initialize( list_of_songs = [] )
@list_of_songs = list_of_songs
end
def each
@list_of_songs.each{ |song| yield song }
end
def Collection.parse( text )
parsed_songs = []
text.split("\n").each_slice(4) do |song|
parsed_songs << Song.new(song[0], song[1], song[2])
end
Collection.new( parsed_songs )
end
def artist
@list_of_songs.group_by{ |song| song.artist }.keys
end
def albums
@list_of_songs.group_by{ |song| song.album }.keys
end
def names
@list_of_songs.group_by{ |song| song.name }.keys
end
def filter( creteria )
Collection.new( @list_of_songs.select{ |song| creteria.call(song) } )
end
def adjoin( collection )
list_of_songs = @list_of_songs
collection.each{ |song| list_of_songs << song }
Collection.new( list_of_songs )
end
end
class Criteria
def initialize( filter )
@filter = filter
end
def Criteria.name( name )
Criteria.new( Proc.new{ |song| song.name == name } )
end
def Criteria.artist( artist )
Criteria.new( Proc.new{ |song| song.artist == artist } )
end
def Criteria.album( album )
Criteria.new( Proc.new{ |album| song.album == album } )
end
def !
Criteria.new( Proc.new{ |song| not call(song) } )
end
def &(other)
Criteria.new( Proc.new{ |song| call(song) and other.call(song) } )
end
def |( other )
Criteria.new( Proc.new{ |song| call(song) or other.call(song) } )
end
def call( song )
@filter.call( song )
end
end

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

F....FF....

Failures:

  1) Collection can find all the artists in the collection
     Failure/Error: collection.artists.should =~ [
     NoMethodError:
       undefined method `artists' for #<Collection:0x986023c>
     # /tmp/d20130203-23049-7sn0wg/spec.rb:5: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 album
     Failure/Error: filtered = collection.filter Criteria.album('Live at Blues Alley')
     NameError:
       undefined local variable or method `song' for Criteria:Class
     # /tmp/d20130203-23049-7sn0wg/solution.rb:68:in `block in album'
     # /tmp/d20130203-23049-7sn0wg/solution.rb:84:in `call'
     # /tmp/d20130203-23049-7sn0wg/solution.rb:84:in `call'
     # /tmp/d20130203-23049-7sn0wg/solution.rb:42:in `block in filter'
     # /tmp/d20130203-23049-7sn0wg/solution.rb:42:in `select'
     # /tmp/d20130203-23049-7sn0wg/solution.rb:42:in `filter'
     # /tmp/d20130203-23049-7sn0wg/spec.rb:54: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 return an empty result
     Failure/Error: filtered = collection.filter Criteria.album('The Dark Side of the Moon')
     NameError:
       undefined local variable or method `song' for Criteria:Class
     # /tmp/d20130203-23049-7sn0wg/solution.rb:68:in `block in album'
     # /tmp/d20130203-23049-7sn0wg/solution.rb:84:in `call'
     # /tmp/d20130203-23049-7sn0wg/solution.rb:84:in `call'
     # /tmp/d20130203-23049-7sn0wg/solution.rb:42:in `block in filter'
     # /tmp/d20130203-23049-7sn0wg/solution.rb:42:in `select'
     # /tmp/d20130203-23049-7sn0wg/solution.rb:42:in `filter'
     # /tmp/d20130203-23049-7sn0wg/spec.rb:59: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.01024 seconds
11 examples, 3 failures

Failed examples:

rspec /tmp/d20130203-23049-7sn0wg/spec.rb:4 # Collection can find all the artists in the collection
rspec /tmp/d20130203-23049-7sn0wg/spec.rb:53 # Collection can be filtered by album
rspec /tmp/d20130203-23049-7sn0wg/spec.rb:58 # Collection can return an empty result

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

Николай обнови решението на 31.10.2012 11:01 (преди около 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
+
+ def initialize( list_of_songs = [] )
+ @list_of_songs = list_of_songs
+ end
+
+ def each
+ @list_of_songs.each{ |song| yield song }
+ end
+
+ def Collection.parse( text )
+ parsed_songs = []
+ text.split("\n").each_slice(4) do |song|
+ parsed_songs << Song.new(song[0], song[1], song[2])
+ end
+ Collection.new( parsed_songs )
+ end
+
+ def artist
+ @list_of_songs.group_by{ |song| song.artist }.keys
+ end
+
+ def albums
+ @list_of_songs.group_by{ |song| song.album }.keys
+ end
+
+ def names
+ @list_of_songs.group_by{ |song| song.name }.keys
+ end
+
+ def filter( creteria )
+ Collection.new( @list_of_songs.select{ |song| creteria.call(song) } )
+ end
+
+ def adjoin( collection )
+ list_of_songs = @list_of_songs
+ collection.each{ |song| list_of_songs << song }
+ Collection.new( list_of_songs )
+ end
+
+end
+
+class Criteria
+
+ def initialize( filter )
+ @filter = filter
+ end
+
+ def Criteria.name( name )
+ Criteria.new( Proc.new{ |song| song.name == name } )
+ end
+
+ def Criteria.artist( artist )
+ Criteria.new( Proc.new{ |song| song.artist == artist } )
+ end
+
+ def Criteria.album( album )
+ Criteria.new( Proc.new{ |album| song.album == album } )
+ end
+
+ def !
+ Criteria.new( Proc.new{ |song| not call(song) } )
+ end
+
+ def &(other)
+ Criteria.new( Proc.new{ |song| call(song) and other.call(song) } )
+ end
+
+ def |( other )
+ Criteria.new( Proc.new{ |song| call(song) or other.call(song) } )
+ end
+
+ def call( song )
+ @filter.call( song )
+ end
+end