Решение на Трета задача от Ивайло Георгиев

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

Към профила на Ивайло Георгиев

Резултати

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

Код

module RBFS
class File
def initialize(object = nil)
@object = object
end
def data_type
case @object
when String then :string
when Fixnum,Float then :number
when Symbol then :symbol
when NilClass then :nil
else :boolean
end
end
def data= (other)
@object = other
@object.to_s
end
def data
@object.to_s
end
def serialize
"#{data_type}:#{data}"
end
def self.parse(string_data)
RBFS::File.new(
case string_data.split(":").first
when 'nil' then nil
when 'string' then string_data.split(":").drop(1).join(" ").to_s
when 'number' then eval(string_data.split(":").drop(1).join(" "))
when 'symbol' then string_data.split(":").drop(1).join(" ").to_sym
when string_data.split(":").drop(1).join(" ").to_s == 'true' then true
else false
end)
end
end
class My_Hash
def initialize
@hash = Hash.new{ |hash, key| hash[key] = Hash.new }
end
end
class Directory < My_Hash
def files
@hash['file']
end
def directories
@hash['directories']
end
def [](name)
if directories.has_key?(name)
directories[name]
elsif files.has_key?(name)
files[name]
else
nil
end
end
def add_file(name,file)
unless name.include? ":"
files[name] = file
end
end
def add_directory(name,directory = RBFS::Directory.new)
unless name.include? ":"
directories[name] = directory
end
end
def serialize
"#{files.count}:#{serialize_files.join}#{directories.count}:#{serialize_dir.join}"
end
alias_method :s, :serialize
def serialize_files
k = files.keys
v = files.values
files.each_with_index.map{ |x,i| "#{k[i]}:#{v[i].serialize.size}:#{v[i].serialize}"}
end
def serialize_dir
k = directories.keys
v = directories.values
directories.each_with_index.map{ |x,i| "#{k[i]}:#{v[i].serialize.size}:#{v[i].s}"}
end
end
end

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

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

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

  5) RBFS File has nil as initial data
     Failure/Error: expect(file.data).to eq nil
       
       expected: nil
            got: ""
       
       (compared using ==)
     # /tmp/d20141111-26053-vmknh5/spec.rb:203: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)>'

  6) RBFS File data type nil can be parsed
     Failure/Error: expect(file.data     ).to eq nil
       
       expected: nil
            got: ""
       
       (compared using ==)
     # /tmp/d20141111-26053-vmknh5/spec.rb:231: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-vmknh5/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)>'

  8) 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-vmknh5/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)>'

  9) 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-vmknh5/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)>'

  10) 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-vmknh5/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)>'

  11) RBFS File data type boolean true can be parsed
     Failure/Error: expect(file.data     ).to eq true
       
       expected: true
            got: "false"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -true
       +"false"
     # /tmp/d20141111-26053-vmknh5/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)>'

  12) 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-vmknh5/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.05589 seconds
44 examples, 12 failures

Failed examples:

rspec /tmp/d20141111-26053-vmknh5/spec.rb:100 # RBFS Directory serialization ::parse can parse empty directories
rspec /tmp/d20141111-26053-vmknh5/spec.rb:107 # RBFS Directory serialization ::parse can parse directories with files
rspec /tmp/d20141111-26053-vmknh5/spec.rb:116 # RBFS Directory serialization ::parse can parse directory trees without files
rspec /tmp/d20141111-26053-vmknh5/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively
rspec /tmp/d20141111-26053-vmknh5/spec.rb:202 # RBFS File has nil as initial data
rspec /tmp/d20141111-26053-vmknh5/spec.rb:229 # RBFS File data type nil can be parsed
rspec /tmp/d20141111-26053-vmknh5/spec.rb:254 # RBFS File data type string can parse a string with colons
rspec /tmp/d20141111-26053-vmknh5/spec.rb:273 # RBFS File data type symbol can be parsed
rspec /tmp/d20141111-26053-vmknh5/spec.rb:292 # RBFS File data type number can be parsed
rspec /tmp/d20141111-26053-vmknh5/spec.rb:311 # RBFS File data type float number can be parsed
rspec /tmp/d20141111-26053-vmknh5/spec.rb:331 # RBFS File data type boolean true can be parsed
rspec /tmp/d20141111-26053-vmknh5/spec.rb:350 # RBFS File data type boolean false can be parsed

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

Ивайло обнови решението на 08.11.2014 23:30 (преди около 10 години)

+module RBFS
+
+ class File
+
+ def initialize(object = nil)
+ @object = object
+ end
+
+ def data_type
+ case @object
+ when String then :string
+ when Fixnum,Float then :number
+ when Symbol then :symbol
+ when NilClass then :nil
+ else :boolean
+ end
+ end
+
+ def data= (other)
+ @object = other
+ @object.to_s
+ end
+
+ def data
+ @object.to_s
+ end
+
+ def serialize
+ "#{data_type}:#{data}"
+ end
+
+ def self.parse(string_data)
+ RBFS::File.new(
+ case string_data.split(":").first
+ when 'nil' then nil
+ when 'string' then string_data.split(":").drop(1).join(" ").to_s
+ when 'number' then eval(string_data.split(":").drop(1).join(" "))
+ when 'symbol' then string_data.split(":").drop(1).join(" ").to_sym
+ when string_data.split(":").drop(1).join(" ").to_s == 'true' then true
+ else false
+ end)
+ end
+
+ end
+
+ class My_Hash
+ def initialize
+ @hash = Hash.new{ |hash, key| hash[key] = Hash.new }
+ end
+
+ end
+
+ class Directory < My_Hash
+
+ def files
+ @hash['file']
+ end
+
+ def directories
+ @hash['directories']
+ end
+
+ def [](name)
+ if directories.has_key?(name)
+ directories[name]
+ elsif files.has_key?(name)
+ files[name]
+ else
+ nil
+ end
+ end
+
+ def add_file(name,file)
+ unless name.include? ":"
+ files[name] = file
+ end
+ end
+
+ def add_directory(name,directory = RBFS::Directory.new)
+ unless name.include? ":"
+ directories[name] = directory
+ end
+ end
+
+ def serialize
+ "#{files.count}:#{serialize_files.join}#{directories.count}:#{serialize_dir.join}"
+ end
+ alias_method :s, :serialize
+
+ def serialize_files
+ k = files.keys
+ v = files.values
+
+ files.each_with_index.map{ |x,i| "#{k[i]}:#{v[i].serialize.size}:#{v[i].serialize}"}
+ end
+
+ def serialize_dir
+ k = directories.keys
+ v = directories.values
+
+ directories.each_with_index.map{ |x,i| "#{k[i]}:#{v[i].serialize.size}:#{v[i].s}"}
+ end
+
+ end
+
+
+end

Ивайло обнови решението на 08.11.2014 23:31 (преди около 10 години)

module RBFS
class File
def initialize(object = nil)
@object = object
end
def data_type
case @object
when String then :string
when Fixnum,Float then :number
when Symbol then :symbol
when NilClass then :nil
else :boolean
end
end
def data= (other)
@object = other
@object.to_s
end
def data
@object.to_s
end
def serialize
"#{data_type}:#{data}"
end
def self.parse(string_data)
RBFS::File.new(
case string_data.split(":").first
when 'nil' then nil
when 'string' then string_data.split(":").drop(1).join(" ").to_s
when 'number' then eval(string_data.split(":").drop(1).join(" "))
when 'symbol' then string_data.split(":").drop(1).join(" ").to_sym
when string_data.split(":").drop(1).join(" ").to_s == 'true' then true
else false
end)
end
end
class My_Hash
def initialize
@hash = Hash.new{ |hash, key| hash[key] = Hash.new }
end
end
class Directory < My_Hash
def files
@hash['file']
end
def directories
@hash['directories']
end
def [](name)
if directories.has_key?(name)
directories[name]
elsif files.has_key?(name)
files[name]
else
nil
end
end
def add_file(name,file)
unless name.include? ":"
files[name] = file
end
end
def add_directory(name,directory = RBFS::Directory.new)
unless name.include? ":"
directories[name] = directory
end
end
def serialize
"#{files.count}:#{serialize_files.join}#{directories.count}:#{serialize_dir.join}"
end
alias_method :s, :serialize
def serialize_files
k = files.keys
v = files.values
files.each_with_index.map{ |x,i| "#{k[i]}:#{v[i].serialize.size}:#{v[i].serialize}"}
end
def serialize_dir
- k = directories.keys
- v = directories.values
+ k = directories.keys
+ v = directories.values
directories.each_with_index.map{ |x,i| "#{k[i]}:#{v[i].serialize.size}:#{v[i].s}"}
end
end
end

Ивайло обнови решението на 08.11.2014 23:36 (преди около 10 години)

module RBFS
class File
def initialize(object = nil)
@object = object
end
def data_type
case @object
when String then :string
when Fixnum,Float then :number
when Symbol then :symbol
when NilClass then :nil
else :boolean
end
end
def data= (other)
@object = other
@object.to_s
end
def data
@object.to_s
end
def serialize
"#{data_type}:#{data}"
end
def self.parse(string_data)
RBFS::File.new(
case string_data.split(":").first
- when 'nil' then nil
- when 'string' then string_data.split(":").drop(1).join(" ").to_s
- when 'number' then eval(string_data.split(":").drop(1).join(" "))
- when 'symbol' then string_data.split(":").drop(1).join(" ").to_sym
- when string_data.split(":").drop(1).join(" ").to_s == 'true' then true
- else false
- end)
+ when 'nil' then nil
+ when 'string' then string_data.split(":").drop(1).join(" ").to_s
+ when 'number' then eval(string_data.split(":").drop(1).join(" "))
+ when 'symbol' then string_data.split(":").drop(1).join(" ").to_sym
+ when string_data.split(":").drop(1).join(" ").to_s == 'true' then true
+ else false
+ end)
end
end
class My_Hash
def initialize
@hash = Hash.new{ |hash, key| hash[key] = Hash.new }
end
end
class Directory < My_Hash
def files
@hash['file']
end
def directories
@hash['directories']
end
def [](name)
if directories.has_key?(name)
directories[name]
elsif files.has_key?(name)
files[name]
else
nil
end
end
def add_file(name,file)
unless name.include? ":"
files[name] = file
end
end
def add_directory(name,directory = RBFS::Directory.new)
unless name.include? ":"
directories[name] = directory
end
end
def serialize
"#{files.count}:#{serialize_files.join}#{directories.count}:#{serialize_dir.join}"
end
alias_method :s, :serialize
def serialize_files
k = files.keys
v = files.values
files.each_with_index.map{ |x,i| "#{k[i]}:#{v[i].serialize.size}:#{v[i].serialize}"}
end
def serialize_dir
k = directories.keys
v = directories.values
directories.each_with_index.map{ |x,i| "#{k[i]}:#{v[i].serialize.size}:#{v[i].s}"}
end
end
end