Решение на Трета задача от Гюлджан Купен

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

Към профила на Гюлджан Купен

Резултати

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

Код

module RBFS
class File
attr_accessor :data
def initialize(data = nil)
@data = data
end
def data_type
case @data.class.to_s
when 'Fixnum', 'Float' then @data_type = :number
when 'TrueClass', 'FalseClass' then @data_type = :boolean
when 'NilClass' then @data_type = :nil
else @data_type = @data.class.to_s.downcase.intern
end
end
def serialize
data_type.to_s + ':' + @data.to_s
end
def self.parse(string_data)
array = string_data.partition(':')
case array[0]
when 'number' then File.new(array[2].to_f)
when 'nil' then File.new(nil)
when 'string' then File.new(array[2])
when 'symbol' then File.new(array[2].intern)
when 'boolean' then File.new(array[2] == 'true')
end
end
end
class Directory
def initialize(hash = {})
@directory = hash
end
def add_file(name, file)
@directory[name] = file
end
def add_directory(name, directory = Directory.new)
@directory[name] = directory
end
def files
@directory.select{|key, value| value.is_a? File}
end
def directories
@directory.select{|key, value| value.is_a? Directory}
end
def [](name)
@directory[name]
end
def serialize
result = files.length.to_s + ':'
files.each do |key, value|
result += key + ':' + value.serialize.length.to_s + ':' + value.serialize
end
result += directories.length.to_s + ':'
directories.each do |key, value|
result += key + ':' + value.serialize.length.to_s + ':' + value.serialize
end
result
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-79ue6w/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-79ue6w/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-79ue6w/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-79ue6w/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.05817 seconds
44 examples, 4 failures

Failed examples:

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

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

Гюлджан обнови решението на 06.11.2014 14:25 (преди над 9 години)

+module RBFS
+ class File
+ attr_accessor :data
+
+ def initialize(data = nil)
+ @data = data
+ end
+
+ def data_type
+ case @data.class.to_s
+ when "Fixnum", "Float" then @data_type = :number
+ when "TrueClass", "FalseClass" then @data_type = :boolean
+ when "NilClass" then @data_type = :nil
+ else @data_type = @data.class.to_s.downcase.intern
+ end
+ end
+
+ def serialize
+ data_type.to_s + ":" + @data.to_s
+ end
+
+ def self.parse(string_data)
+ array = string_data.partition(':')
+ case array[0]
+ when 'number' then File.new(array[2].to_f)
+ when 'nil' then File.new(nil)
+ when 'string' then File.new(array[2])
+ when 'symbol' then File.new(array[2].intern)
+ when 'boolean' then File.new(array[2] == 'true')
+ end
+ end
+ end
+
+ class Directory
+ def initialize(hash = {})
+ @directory = hash
+ end
+
+ def add_file(name, file)
+ @directory[name] = file
+ end
+
+ def add_directory(name, directory = Directory.new)
+ @directory[name] = directory
+ end
+
+ def files
+ @directory.select{|key, value| value.is_a? File}
+ end
+
+ def directories
+ @directory.select{|key, value| value.is_a? Directory}
+ end
+
+ def [](name)
+ @directory[name]
+ end
+
+ def serialize
+ str = files.length.to_s + ':'
+ files.each do |key, value|
+ str += key + ':' + value.serialize.length.to_s + ':' + value.serialize
+ end
+ str += directories.length.to_s + ':'
+ directories.each do |key, value|
+ str += key + ':' + value.serialize.length.to_s + ':' + value.serialize
+ end
+ str
+ end
+ end
+end

Гюлджан обнови решението на 09.11.2014 21:33 (преди над 9 години)

module RBFS
class File
attr_accessor :data
def initialize(data = nil)
@data = data
end
def data_type
case @data.class.to_s
- when "Fixnum", "Float" then @data_type = :number
- when "TrueClass", "FalseClass" then @data_type = :boolean
- when "NilClass" then @data_type = :nil
+ when 'Fixnum', 'Float' then @data_type = :number
+ when 'TrueClass', 'FalseClass' then @data_type = :boolean
+ when 'NilClass' then @data_type = :nil
else @data_type = @data.class.to_s.downcase.intern
end
end
def serialize
- data_type.to_s + ":" + @data.to_s
+ data_type.to_s + ':' + @data.to_s
end
def self.parse(string_data)
array = string_data.partition(':')
case array[0]
when 'number' then File.new(array[2].to_f)
when 'nil' then File.new(nil)
when 'string' then File.new(array[2])
when 'symbol' then File.new(array[2].intern)
when 'boolean' then File.new(array[2] == 'true')
end
end
end
class Directory
def initialize(hash = {})
@directory = hash
end
def add_file(name, file)
@directory[name] = file
end
def add_directory(name, directory = Directory.new)
@directory[name] = directory
end
def files
@directory.select{|key, value| value.is_a? File}
end
def directories
@directory.select{|key, value| value.is_a? Directory}
end
def [](name)
@directory[name]
end
def serialize
- str = files.length.to_s + ':'
+ result = files.length.to_s + ':'
files.each do |key, value|
- str += key + ':' + value.serialize.length.to_s + ':' + value.serialize
+ result += key + ':' + value.serialize.length.to_s + ':' + value.serialize
end
- str += directories.length.to_s + ':'
+ result += directories.length.to_s + ':'
directories.each do |key, value|
- str += key + ':' + value.serialize.length.to_s + ':' + value.serialize
+ result += key + ':' + value.serialize.length.to_s + ':' + value.serialize
end
- str
+ result
end
end
end