Решение на Трета задача от Нели Василева

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

Към профила на Нели Василева

Резултати

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

Код

module RBFS
end
class RBFS::File
attr_reader :data_type
attr_reader :data
def initialize object=nil
@data_type = get_type object
@data = object
end
def data=(object)
@data = object
@data_type = get_type object
end
def serialize
"#{@data_type.to_s}:#{@data.to_s}"
end
def self.parse(string_data)
fields = string_data.split(':')
case fields[0]
when "nil" then new
when "string" then new fields[1]
when "boolean" then new to_boolean(fields[1])
when "symbol" then new fields[1].to_sym
when "number" then new to_number(fields[1])
end
end
private
def get_type(object)
return :nil if object.nil?
return :number if object.is_a?(Numeric)
return :boolean if (object == true || object == false)
object.class.name.downcase.to_sym
end
def self.to_boolean(string)
case string
when 'true' then true
when 'false' then false
end
end
def self.to_number(string)
return string.to_f unless string.match('\.|e').nil?
string.to_i
end
end
class RBFS::Directory
attr_reader :files
attr_reader :directories
def initialize
@files = {}
@directories = {}
end
def add_file(name, file)
@files[name] = file
end
def add_directory(name, directory = nil)
@directories[name] = directory
end
def [](name)
@directories[name] || @files[name]
end
def serialize
"#{@files.size}:#{contents :file}#{@directories.size}:#{contents :directory}"
end
def self.parse(string_data)
end
private
def contents type
puts type.to_s
builder = ''
collection = {}
case type
when :file then collection = @files
when :directory then collection = @directories
end
collection.each { |key, value| my key, value, builder }
builder
end
def my key, value, builder
contents = value.nil? ? '0:0:' : value.serialize
builder.concat("#{key}:#{contents.length}:#{contents}")
end
end

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

.F..file
directory
....file
directory
.FFFFF..............F...............

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-nvtcyk/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 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-nvtcyk/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)>'

  3) RBFS Directory serialization ::parse can parse empty directories
     Failure/Error: expect(parsed_directory.files      ).to eq({})
     NoMethodError:
       undefined method `files' for nil:NilClass
     # /tmp/d20141111-26053-nvtcyk/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)>'

  4) RBFS Directory serialization ::parse can parse directories with files
     Failure/Error: expect(parsed_directory.files.size     ).to eq    2
     NoMethodError:
       undefined method `files' for nil:NilClass
     # /tmp/d20141111-26053-nvtcyk/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)>'

  5) RBFS Directory serialization ::parse can parse directory trees without files
     Failure/Error: expect(parsed_directory['dir1']        ).to be_an RBFS::Directory
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20141111-26053-nvtcyk/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)>'

  6) RBFS Directory serialization ::parse can parse directories recursively
     Failure/Error: expect(parsed_directory.files.size     ).to eq 2
     NoMethodError:
       undefined method `files' for nil:NilClass
     # /tmp/d20141111-26053-nvtcyk/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)>'

  7) 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-nvtcyk/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)>'

Finished in 0.04136 seconds
44 examples, 7 failures

Failed examples:

rspec /tmp/d20141111-26053-nvtcyk/spec.rb:149 # RBFS Directory can create empty directory
rspec /tmp/d20141111-26053-nvtcyk/spec.rb:84 # RBFS Directory serialization #serialize can serialize multiple directories recursively
rspec /tmp/d20141111-26053-nvtcyk/spec.rb:100 # RBFS Directory serialization ::parse can parse empty directories
rspec /tmp/d20141111-26053-nvtcyk/spec.rb:107 # RBFS Directory serialization ::parse can parse directories with files
rspec /tmp/d20141111-26053-nvtcyk/spec.rb:116 # RBFS Directory serialization ::parse can parse directory trees without files
rspec /tmp/d20141111-26053-nvtcyk/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively
rspec /tmp/d20141111-26053-nvtcyk/spec.rb:254 # RBFS File data type string can parse a string with colons

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

Нели обнови решението на 10.11.2014 02:13 (преди около 10 години)

+module RBFS
+end
+
+class RBFS::File
+
+ attr_reader :data_type
+ attr_reader :data
+
+ def initialize object=nil
+ @data_type = get_type object
+ @data = object
+ end
+
+ def data=(object)
+ @data = object
+ @data_type = get_type object
+ end
+
+ def serialize
+ "#{@data_type.to_s}:#{@data.to_s}"
+ end
+
+ def self.parse(string_data)
+ fields = string_data.split(':')
+ case fields[0]
+ when "nil" then new
+ when "string" then new fields[1]
+ when "boolean" then new to_boolean(fields[1])
+ when "symbol" then new fields[1].to_sym
+ when "number" then new to_number(fields[1])
+ end
+ end
+
+ private
+ def get_type(object)
+ return :nil if object.nil?
+ return :number if object.is_a?(Numeric)
+ return :boolean if (object == true || object == false)
+ object.class.name.downcase.to_sym
+ end
+
+ def self.to_boolean(string)
+ case string
+ when 'true' then true
+ when 'false' then false
+ end
+ end
+
+ def self.to_number(string)
+ return string.to_f unless string.match('\.|e').nil?
+ string.to_i
+ end
+
+end
+
+class RBFS::Directory
+
+ attr_reader :files
+ attr_reader :directories
+
+ def initialize
+ @files = {}
+ @directories = {}
+ end
+
+ def add_file(name, file)
+ @files[name] = file
+ end
+
+ def add_directory(name, directory = nil)
+ @directories[name] = directory
+ end
+
+ def [](name)
+ @directories[name] || @files[name]
+ end
+
+ def serialize
+ "#{@files.size}:#{contents :file}#{@directories.size}:#{contents :directory}"
+ end
+
+ def self.parse(string_data)
+
+ end
+
+ private
+ def contents type
+ puts type.to_s
+ builder = ''
+ collection = {}
+ #name:size:containment
+ case type
+ when :file then collection = @files
+ when :directory then collection = @directories
+ end
+ collection.each { |key, value| my key, value, builder }
+ builder
+ end
+
+ def my key, value, builder
+ contents = value.nil? ? '0:0:' : value.serialize
+ builder.concat("#{key}:#{contents.length}:#{contents}")
+ end
+end

Нели обнови решението на 10.11.2014 12:51 (преди около 10 години)

module RBFS
end
class RBFS::File
attr_reader :data_type
attr_reader :data
def initialize object=nil
@data_type = get_type object
@data = object
end
def data=(object)
@data = object
@data_type = get_type object
end
def serialize
"#{@data_type.to_s}:#{@data.to_s}"
end
def self.parse(string_data)
fields = string_data.split(':')
case fields[0]
when "nil" then new
when "string" then new fields[1]
when "boolean" then new to_boolean(fields[1])
when "symbol" then new fields[1].to_sym
when "number" then new to_number(fields[1])
end
end
private
def get_type(object)
return :nil if object.nil?
return :number if object.is_a?(Numeric)
return :boolean if (object == true || object == false)
object.class.name.downcase.to_sym
end
def self.to_boolean(string)
case string
when 'true' then true
when 'false' then false
end
end
def self.to_number(string)
return string.to_f unless string.match('\.|e').nil?
string.to_i
end
end
class RBFS::Directory
attr_reader :files
attr_reader :directories
def initialize
@files = {}
@directories = {}
end
def add_file(name, file)
@files[name] = file
end
def add_directory(name, directory = nil)
@directories[name] = directory
end
def [](name)
@directories[name] || @files[name]
end
def serialize
"#{@files.size}:#{contents :file}#{@directories.size}:#{contents :directory}"
end
def self.parse(string_data)
end
private
def contents type
puts type.to_s
builder = ''
collection = {}
- #name:size:containment
case type
when :file then collection = @files
when :directory then collection = @directories
end
collection.each { |key, value| my key, value, builder }
builder
end
def my key, value, builder
contents = value.nil? ? '0:0:' : value.serialize
builder.concat("#{key}:#{contents.length}:#{contents}")
end
end