Решение на Трета задача от Габриела Лухова

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

Към профила на Габриела Лухова

Резултати

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

Код

module RBFS
class File
attr_accessor :data
def initialize(object = nil)
@data = object
end
def data_type
case @data
when String then @data_type = :string
when Numeric then @data_type = :number
when Symbol then @data_type = :symbol
when TrueClass then @data_type = :boolean
when FalseClass then @data_type = :boolean
else @data_type = :nil
end
end
def serialize
"#{data_type}:#{@data}"
end
def self.parse(string_data)
array = string_data.split(':', 2)
case array[0]
when "string" then File.new(array[1])
when "symbol" then File.new(array[1].to_sym)
when "number" then File.new(array[1].to_f)
when "boolean" then File.new(array[1] == 'true')
else File.new
end
end
end
class Directory
def initialize
@directory = Hash.new
end
def add_file(name, file)
@directory[name] = file
end
def add_directory(name, directory = Directory.new)
@directory[name] = directory
end
def [](name)
@directory[name]
end
def files
@directory.select do |key, value|
value.is_a? File
end
end
def directories
@directory.select do |key, value|
value.is_a? Directory
end
end
def serialize
string_data = "#{files.length}:"
files.each do |key, value|
string_data += "#{key}:#{value.serialize.length}:" + value.serialize
end
string_data += "#{directories.length}:"
directories.each do |key, value|
string_data += "#{key}:#{value.serialize.length}:" + value.serialize
end
string_data
end
end
end

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

..........FFFF..............................

Failures:

  1) RBFS Directory serialization ::parse can parse empty directories
     Failure/Error: parsed_directory = RBFS::Directory.parse('0:0:')
     NoMethodError:
       undefined method `parse' for RBFS::Directory:Class
     # /tmp/d20141111-26053-1siyku7/spec.rb:101: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: parsed_directory = RBFS::Directory.parse(simple_serialized_string)
     NoMethodError:
       undefined method `parse' for RBFS::Directory:Class
     # /tmp/d20141111-26053-1siyku7/spec.rb:108: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: parsed_directory = RBFS::Directory.parse('0:2:dir1:15:0:1:dir2:4:0:0:dir3:4:0:0:')
     NoMethodError:
       undefined method `parse' for RBFS::Directory:Class
     # /tmp/d20141111-26053-1siyku7/spec.rb:117: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: parsed_directory = RBFS::Directory.parse(recursive_serialized_string)
     NoMethodError:
       undefined method `parse' for RBFS::Directory:Class
     # /tmp/d20141111-26053-1siyku7/spec.rb:125: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.04169 seconds
44 examples, 4 failures

Failed examples:

rspec /tmp/d20141111-26053-1siyku7/spec.rb:100 # RBFS Directory serialization ::parse can parse empty directories
rspec /tmp/d20141111-26053-1siyku7/spec.rb:107 # RBFS Directory serialization ::parse can parse directories with files
rspec /tmp/d20141111-26053-1siyku7/spec.rb:116 # RBFS Directory serialization ::parse can parse directory trees without files
rspec /tmp/d20141111-26053-1siyku7/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively

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

Габриела обнови решението на 08.11.2014 16:52 (преди около 10 години)

+module RBFS
+ class File
+ attr_accessor :data
+
+ def initialize(object = nil)
+ @data = object
+ end
+
+ def data_type
+ case @data
+ when String then @data_type = :string
+ when Numeric then @data_type = :number
+ when Symbol then @data_type = :symbol
+ when TrueClass then @data_type = :boolean
+ when FalseClass then @data_type = :boolean
+ else @data_type = :nil
+ end
+ end
+
+ def serialize
+ "#{data_type}:#{@data}"
+ end
+
+ def self.parse(string_data)
+ array = string_data.split(':', 2)
+ case array[0]
+ when "string" then File.new(array[1])
+ when "symbol" then File.new(array[1].to_sym)
+ when "number" then File.new(array[1].to_f)
+ when "boolean" then File.new(array[1] == 'true')
+ else File.new
+ end
+ end
+ end
+
+ class Directory
+ def initialize
+ @directory = Hash.new
+ end
+
+ def add_file(name, file)
+ @directory[name] = file
+ end
+
+ def add_directory(name, directory = Directory.new)
+ @directory[name] = directory
+ end
+
+ def [](name)
+ @directory[name]
+ end
+
+ def files
+ @directory.select do |key, value|
+ value.is_a? File
+ end
+ end
+
+ def directories
+ @directory.select do |key, value|
+ value.is_a? Directory
+ end
+ end
+
+ def serialize
+ string_data = "#{files.length}:"
+ files.each do |key, value|
+ string_data += "#{key}:#{value.serialize.length}:" + value.serialize
+ end
+ string_data += "#{directories.length}:"
+ directories.each do |key, value|
+ string_data += "#{key}:#{value.serialize.length}:" + value.serialize
+ end
+ string_data
+ end
+ end
+end