Решение на Трета задача от Мартина Радева

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

Към профила на Мартина Радева

Резултати

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

Код

module RBFS
class File
attr_accessor :data
def initialize(*data)
@data = data[0]
end
def data_type
case @data
when String then @data_type = :string
when Symbol then @data_type = :symbol
when Numeric then @data_type = :number
when (TrueClass or FalseClass) then @data_type = :boolean
else @data_type = :nil
end
end
def serialize
self.data_type.to_s + ':' + @data.to_s
end
def self.parse(string)
File.new string.split(":")[1]
end
end
class Directory
attr_reader :directories
attr_reader :files
attr_accessor :name
def initialize
@directories = {}
@files = {}
end
def add_file(name, file)
if !name.include? ":" then @files.merge!({ name => file }) end
end
def add_directory(name, *directory)
@directories.merge!(directory ? {name => directory[0]} : {name => Directory.new})
end
def [](key)
@directories[key] or @files[key]
end
def serialize(directory)
"return string to save instead of object"
end
def self.parse(string)
"turns string into a Directory object"
end
end
end

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

.F..F...FFFFFF..............F..F..F..F..FFFF

Failures:

  1) RBFS Directory can create empty directory
     Failure/Error: expect(directory.directories['home']).to be_an RBFS::Directory
       expected nil to be a kind of RBFS::Directory
     # /tmp/d20141111-26053-1cn6x3b/spec.rb:153:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) RBFS Directory without files can be serialized
     Failure/Error: expect(directory.serialize).to eq '0:0:'
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20141111-26053-1cn6x3b/solution.rb:50:in `serialize'
     # /tmp/d20141111-26053-1cn6x3b/spec.rb:11:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) RBFS Directory serialization #serialize can serialize
     Failure/Error: expect(directory.serialize).to eq simple_serialized_string
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20141111-26053-1cn6x3b/solution.rb:50:in `serialize'
     # /tmp/d20141111-26053-1cn6x3b/spec.rb:81:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) RBFS Directory serialization #serialize can serialize multiple directories recursively
     Failure/Error: directory['rbfs'].add_file         'solution.rb', RBFS::File.new(:hidden)
     NoMethodError:
       undefined method `add_file' for nil:NilClass
     # /tmp/d20141111-26053-1cn6x3b/spec.rb:91:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) RBFS Directory serialization ::parse can parse empty directories
     Failure/Error: expect(parsed_directory.files      ).to eq({})
     NoMethodError:
       undefined method `files' for "turns string into a Directory object":String
     # /tmp/d20141111-26053-1cn6x3b/spec.rb:103:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) RBFS Directory serialization ::parse can parse directories with files
     Failure/Error: expect(parsed_directory.files.size     ).to eq    2
     NoMethodError:
       undefined method `files' for "turns string into a Directory object":String
     # /tmp/d20141111-26053-1cn6x3b/spec.rb:110:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  7) RBFS Directory serialization ::parse can parse directory trees without files
     Failure/Error: expect(parsed_directory['dir1']        ).to be_an RBFS::Directory
       expected nil to be a kind of RBFS::Directory
     # /tmp/d20141111-26053-1cn6x3b/spec.rb:119:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  8) RBFS Directory serialization ::parse can parse directories recursively
     Failure/Error: expect(parsed_directory.files.size     ).to eq 2
     NoMethodError:
       undefined method `files' for "turns string into a Directory object":String
     # /tmp/d20141111-26053-1cn6x3b/spec.rb:127:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  9) RBFS File data type string can parse a string with colons
     Failure/Error: expect(file.data     ).to eq 'Hay :)'
       
       expected: "Hay :)"
            got: "Hay "
       
       (compared using ==)
     # /tmp/d20141111-26053-1cn6x3b/spec.rb:257:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  10) RBFS File data type symbol can be parsed
     Failure/Error: expect(file.data     ).to eq :hello
       
       expected: :hello
            got: "hello"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:hello
       +"hello"
     # /tmp/d20141111-26053-1cn6x3b/spec.rb:276:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  11) RBFS File data type number can be parsed
     Failure/Error: expect(file.data     ).to eq 1234
       
       expected: 1234
            got: "1234"
       
       (compared using ==)
     # /tmp/d20141111-26053-1cn6x3b/spec.rb:295:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  12) RBFS File data type float number can be parsed
     Failure/Error: expect(file.data     ).to eq 3.14
       
       expected: 3.14
            got: "3.14"
       
       (compared using ==)
     # /tmp/d20141111-26053-1cn6x3b/spec.rb:314:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  13) RBFS File data type boolean true can be parsed
     Failure/Error: expect(file.data     ).to eq true
       
       expected: true
            got: "true"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -true
       +"true"
     # /tmp/d20141111-26053-1cn6x3b/spec.rb:334:in `block (6 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  14) RBFS File data type boolean false can be detected
     Failure/Error: expect(file.data_type).to eq :boolean
       
       expected: :boolean
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:boolean
       +:nil
     # /tmp/d20141111-26053-1cn6x3b/spec.rb:342:in `block (6 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  15) RBFS File data type boolean false can be serialized
     Failure/Error: expect(file.serialize).to eq 'boolean:false'
       
       expected: "boolean:false"
            got: "nil:false"
       
       (compared using ==)
     # /tmp/d20141111-26053-1cn6x3b/spec.rb:347:in `block (6 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  16) RBFS File data type boolean false can be parsed
     Failure/Error: expect(file.data     ).to eq false
       
       expected: false
            got: "false"
       
       (compared using ==)
     # /tmp/d20141111-26053-1cn6x3b/spec.rb:353:in `block (6 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.0451 seconds
44 examples, 16 failures

Failed examples:

rspec /tmp/d20141111-26053-1cn6x3b/spec.rb:149 # RBFS Directory can create empty directory
rspec /tmp/d20141111-26053-1cn6x3b/spec.rb:10 # RBFS Directory without files can be serialized
rspec /tmp/d20141111-26053-1cn6x3b/spec.rb:76 # RBFS Directory serialization #serialize can serialize
rspec /tmp/d20141111-26053-1cn6x3b/spec.rb:84 # RBFS Directory serialization #serialize can serialize multiple directories recursively
rspec /tmp/d20141111-26053-1cn6x3b/spec.rb:100 # RBFS Directory serialization ::parse can parse empty directories
rspec /tmp/d20141111-26053-1cn6x3b/spec.rb:107 # RBFS Directory serialization ::parse can parse directories with files
rspec /tmp/d20141111-26053-1cn6x3b/spec.rb:116 # RBFS Directory serialization ::parse can parse directory trees without files
rspec /tmp/d20141111-26053-1cn6x3b/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively
rspec /tmp/d20141111-26053-1cn6x3b/spec.rb:254 # RBFS File data type string can parse a string with colons
rspec /tmp/d20141111-26053-1cn6x3b/spec.rb:273 # RBFS File data type symbol can be parsed
rspec /tmp/d20141111-26053-1cn6x3b/spec.rb:292 # RBFS File data type number can be parsed
rspec /tmp/d20141111-26053-1cn6x3b/spec.rb:311 # RBFS File data type float number can be parsed
rspec /tmp/d20141111-26053-1cn6x3b/spec.rb:331 # RBFS File data type boolean true can be parsed
rspec /tmp/d20141111-26053-1cn6x3b/spec.rb:340 # RBFS File data type boolean false can be detected
rspec /tmp/d20141111-26053-1cn6x3b/spec.rb:345 # RBFS File data type boolean false can be serialized
rspec /tmp/d20141111-26053-1cn6x3b/spec.rb:350 # RBFS File data type boolean false can be parsed

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

Мартина обнови решението на 04.11.2014 22:51 (преди около 10 години)

+module RBFS
+ class File
+ attr_accessor :data
+
+ def initialize(*data)
+ @data = data[0]
+ end
+
+ def data_type
+ case @data
+ when String then @data_type = :string
+ when Symbol then @data_type = :symbol
+ when Numeric then @data_type = :number
+ when (TrueClass or FalseClass) then @data_type = :boolean
+ else @data_type = :nil
+ end
+ end
+
+ def serialize
+ self.data_type.to_s + ':' + @data.to_s
+ end
+
+ def self.parse(string)
+ File.new string.split(":")[1]
+ end
+
+ end
+
+ class Directory
+
+ def initialize
+ @directory = {}
+ end
+
+ def add_file(name, file)
+ if !name.include? ":" then @directory.merge!({ name => file }) end
+ end
+
+ def add_directory(name, *directory)
+ if directory then @directory.merge!({ name => directory[0] })
+ else @directory.merge!({ name => Directory.new })
+ end
+ end
+
+ def [](key)
+ @directory[key]
+ end
+
+ def files
+ @directory.select{|key,value| value.class == RBFS::File }
+ end
+
+ def directories
+ @directory.select{|key,value| value.class == RBFS::Directory }
+ end
+
+ def serialize(directory)
+ "return string to save instead of object"
+ end
+
+ def self.parse(string)
+ "turns string into a Directory object"
+ end
+
+ end
+
+end

Мартина обнови решението на 05.11.2014 00:22 (преди около 10 години)

module RBFS
class File
- attr_accessor :data
+ attr_accessor :data, :name
def initialize(*data)
@data = data[0]
end
def data_type
case @data
when String then @data_type = :string
when Symbol then @data_type = :symbol
when Numeric then @data_type = :number
when (TrueClass or FalseClass) then @data_type = :boolean
else @data_type = :nil
end
end
def serialize
self.data_type.to_s + ':' + @data.to_s
end
def self.parse(string)
File.new string.split(":")[1]
end
end
class Directory
+ attr_accessor :name
- def initialize
- @directory = {}
+ def initialize (*name)
+ @directory = []
+ @name = name[0] if name
end
def add_file(name, file)
- if !name.include? ":" then @directory.merge!({ name => file }) end
+ file.name = name
+ @directory << file if !name.include? ":"
end
def add_directory(name, *directory)
- if directory then @directory.merge!({ name => directory[0] })
- else @directory.merge!({ name => Directory.new })
+ if directory.any? then directory[0].name = name
+ @directory << directory[0]
+ else @directory << (Directory.new(name))
end
end
def [](key)
- @directory[key]
+ @directory.select{ |current| current.name == key }[0]
end
def files
- @directory.select{|key,value| value.class == RBFS::File }
+ @directory.lazy.map{|file| {file.name => file}}.reduce({}, :merge).
+ select{ |key,value| value.class == RBFS::File }
end
def directories
- @directory.select{|key,value| value.class == RBFS::Directory }
+ @directory.lazy.map{|directory| {directory.name => directory}}.reduce({}, :merge).
+ select{ |key,value| value.class == RBFS::Directory }
end
def serialize(directory)
- "return string to save instead of object"
+ "turns directory into string"
end
def self.parse(string)
"turns string into a Directory object"
end
end
-end
+end

След коментар от @Георги Ангелов по мейл

  • Наистина това с аргумента на initialize е объркващо. Не виждам причина да го използваш, защото така или иначе взимеш елемента в parse. Освен това не взимаш под внимание типа на нещата във файла при парсването.
  • Нещо, което ще те улесни - можеш ли да се сетиш за друг начин да съхраняваш файловете и поддиректориите в една директория? Говоря за алтернатива на хеша @directory. Помисли и какво ще се случи, ако имаш файл и директория с едно и също име.

Тръгнах в (не знам колко вярната) посока да дефинирам директорията като Array. Това решава проблема с потенциално еднакви имена на обекти в нея.

Ще подскажа малко :)

Първоначалният ти вариант беше на прав път (с хеш), но само един няма да ти свърши работа, заради гореспоменатата причина. Ако имаш един за файлове и един за директории, методите files и directories ще могат да станат на attr_reader-и, а add_directory - едноредов.

Мартина обнови решението на 07.11.2014 10:08 (преди около 10 години)

module RBFS
class File
- attr_accessor :data, :name
+ attr_accessor :data
def initialize(*data)
@data = data[0]
end
def data_type
case @data
when String then @data_type = :string
when Symbol then @data_type = :symbol
when Numeric then @data_type = :number
when (TrueClass or FalseClass) then @data_type = :boolean
else @data_type = :nil
end
end
def serialize
self.data_type.to_s + ':' + @data.to_s
end
def self.parse(string)
File.new string.split(":")[1]
end
end
class Directory
+ attr_reader :directories
+ attr_reader :files
attr_accessor :name
- def initialize (*name)
- @directory = []
- @name = name[0] if name
+ def initialize
+ @directories = {}
+ @files = {}
end
def add_file(name, file)
- file.name = name
- @directory << file if !name.include? ":"
+ if !name.include? ":" then @files.merge!({ name => file }) end
end
def add_directory(name, *directory)
- if directory.any? then directory[0].name = name
- @directory << directory[0]
- else @directory << (Directory.new(name))
+ if directory then @directories.merge!({ name => directory[0] })
+ else @directories.merge!({ name => Directory.new })
end
end
def [](key)
- @directory.select{ |current| current.name == key }[0]
+ @directories[key] or @files[key]
end
- def files
- @directory.lazy.map{|file| {file.name => file}}.reduce({}, :merge).
- select{ |key,value| value.class == RBFS::File }
- end
-
- def directories
- @directory.lazy.map{|directory| {directory.name => directory}}.reduce({}, :merge).
- select{ |key,value| value.class == RBFS::Directory }
- end
-
def serialize(directory)
- "turns directory into string"
+ "return string to save instead of object"
end
def self.parse(string)
"turns string into a Directory object"
end
end
end

Мартина обнови решението на 10.11.2014 08:48 (преди около 10 години)

module RBFS
class File
attr_accessor :data
def initialize(*data)
@data = data[0]
end
def data_type
case @data
when String then @data_type = :string
when Symbol then @data_type = :symbol
when Numeric then @data_type = :number
when (TrueClass or FalseClass) then @data_type = :boolean
else @data_type = :nil
end
end
def serialize
self.data_type.to_s + ':' + @data.to_s
end
def self.parse(string)
File.new string.split(":")[1]
end
-
end
class Directory
attr_reader :directories
attr_reader :files
attr_accessor :name
def initialize
@directories = {}
@files = {}
end
def add_file(name, file)
if !name.include? ":" then @files.merge!({ name => file }) end
end
def add_directory(name, *directory)
- if directory then @directories.merge!({ name => directory[0] })
- else @directories.merge!({ name => Directory.new })
- end
+ @directories.merge!(directory ? {name => directory[0]} : {name => Directory.new})
end
def [](key)
@directories[key] or @files[key]
end
def serialize(directory)
"return string to save instead of object"
end
def self.parse(string)
"turns string into a Directory object"
end
-
end
end