Решение на Трета задача от Светослав Кузманов

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

Към профила на Светослав Кузманов

Резултати

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

Код

module RBFS
class File
attr_accessor :data
attr :data_type
def initialize(object = nil)
@data = object
end
def data_type
case @data
when NilClass then @data_type = :nil
when String then @data_type = :string
when Symbol then @data_type = :symbol
when Fixnum, Float then @data_type = :number
when TrueClass, FalseClass then @data_type = :boolean
end
end
def serialize
"#{self.data_type}:#{@data}"
end
def asdf
@data_type
end
def self.parse(serialized_object)
object_data = serialized_object.split(':')[1]
case serialized_object.split(':')[0]
when 'symbol' then File.new(object_data.to_sym)
when 'boolean' then File.new(object_data == 'true')
when 'number' then File.new(self.to_numeric(object_data))
else File.new(object_data)
end
end
def to_numeric(str)
if str.include? '.'
str.to_f
else
str.to_i
end
end
end
class Directory
def initialize
@files = { }
@directories = { }
end
def add_file(name, file)
@files[name] = file
end
def add_directory(name, directory = Directory.new)
if name.include? ':'
raise ArgumentError, "Directory name can't contain ':'"
else
@directories[name] = directory
end
end
def [](name)
if @directories[name].nil?
@files[name]
else
@directories[name]
end
end
def serialize
serialized = "#{@files.count}:"
@files.each do |name, file|
serialized += "#{name}:#{file.serialize.to_s.length}:#{file.serialize}"
end
serialized += "#{@directories.count}:"
@directories.each do |name, directory|
serialized += "#{name}:#{directory.serialize.size}:" +
"#{directory.serialize}"
end
serialized
end
def self.parse(string_data)
end
def files
@files.dup
end
def directories
@directories.dup
end
end
end

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

..........FFFF..............F.....F..F......

Failures:

  1) 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-kohf0m/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)>'

  2) 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-kohf0m/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)>'

  3) 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-kohf0m/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)>'

  4) 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-kohf0m/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)>'

  5) 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-kohf0m/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)>'

  6) RBFS File data type number can be parsed
     Failure/Error: file = RBFS::File.parse('number:1234')
     NoMethodError:
       undefined method `to_numeric' for RBFS::File:Class
     # /tmp/d20141111-26053-kohf0m/solution.rb:33:in `parse'
     # /tmp/d20141111-26053-kohf0m/spec.rb:293: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 float number can be parsed
     Failure/Error: file = RBFS::File.parse('number:3.14')
     NoMethodError:
       undefined method `to_numeric' for RBFS::File:Class
     # /tmp/d20141111-26053-kohf0m/solution.rb:33:in `parse'
     # /tmp/d20141111-26053-kohf0m/spec.rb:312: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.04191 seconds
44 examples, 7 failures

Failed examples:

rspec /tmp/d20141111-26053-kohf0m/spec.rb:100 # RBFS Directory serialization ::parse can parse empty directories
rspec /tmp/d20141111-26053-kohf0m/spec.rb:107 # RBFS Directory serialization ::parse can parse directories with files
rspec /tmp/d20141111-26053-kohf0m/spec.rb:116 # RBFS Directory serialization ::parse can parse directory trees without files
rspec /tmp/d20141111-26053-kohf0m/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively
rspec /tmp/d20141111-26053-kohf0m/spec.rb:254 # RBFS File data type string can parse a string with colons
rspec /tmp/d20141111-26053-kohf0m/spec.rb:292 # RBFS File data type number can be parsed
rspec /tmp/d20141111-26053-kohf0m/spec.rb:311 # RBFS File data type float number can be parsed

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

Светослав обнови решението на 10.11.2014 10:26 (преди около 10 години)

+module RBFS
+ class File
+ attr_accessor :data
+ attr :data_type
+
+ def initialize(object = nil)
+ @data = object
+ end
+
+ def data_type
+ case @data
+ when NilClass then @data_type = :nil
+ when String then @data_type = :string
+ when Symbol then @data_type = :symbol
+ when Fixnum, Float then @data_type = :number
+ when TrueClass, FalseClass then @data_type = :boolean
+ end
+ end
+
+ def serialize
+ "#{self.data_type}:#{@data}"
+ end
+
+ def asdf
+ @data_type
+ end
+
+ def self.parse(serialized_object)
+ object_data = serialized_object.split(':')[1]
+ case serialized_object.split(':')[0]
+ when 'symbol' then File.new(object_data.to_sym)
+ when 'boolean' then File.new(object_data == 'true')
+ when 'number' then File.new(self.to_numeric(object_data))
+ else File.new(object_data)
+ end
+ end
+
+ def to_numeric(str)
+ if str.include? '.'
+ str.to_f
+ else
+ str.to_i
+ end
+ end
+ end
+
+ class Directory
+
+ def initialize
+ @files = { }
+ @directories = { }
+ end
+
+ def add_file(name, file)
+ @files[name] = file
+ end
+
+ def add_directory(name, directory = Directory.new)
+ if name.include? ':'
+ raise ArgumentError, "Directory name can't contain ':'"
+ else
+ @directories[name] = directory
+ end
+ end
+
+ def [](name)
+ if @directories[name].nil?
+ @files[name]
+ else
+ @directories[name]
+ end
+ end
+
+ def serialize
+ serialized = "#{@files.count}:"
+ @files.each do |name, file|
+ serialized += "#{name}:#{file.serialize.to_s.length}:#{file.serialize}"
+ end
+ serialized += "#{@directories.count}:"
+ @directories.each do |name, directory|
+ serialized += "#{name}:#{directory.serialize.size}:" +
+ "#{directory.serialize}"
+ end
+ serialized
+ end
+
+ def self.parse(string_data)
+
+ end
+
+ def files
+ @files.dup
+ end
+
+ def directories
+ @directories.dup
+ end
+
+ end
+end