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

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

Към профила на Калин Христов

Резултати

  • 2 точки от тестове
  • 0 бонус точки
  • 2 точки общо
  • 17 успешни тест(а)
  • 27 неуспешни тест(а)

Код

module RBFS
class File
attr_accessor :data
def initialize(data = nil)
@data = data
end
def data_type
return :boolean if @data.class == TrueClass
return :boolean if @data.class == FalseClass
return :string if @data.class == String
return :number if @data.class == Fixnum
return :number if @data.class == Float
return :number if @data.class == Bignum
return :symbol if @data.class == Symbol
return :nil if @data.class == NilClass
end
def serialize
data_type.to_s + ':' + data.to_s
end
def self.parse(string_data)
a = File.new
data = a.find_data(string_data)
type = a.find_type(string_data)
a.set_data(type, data)
end
def find_type(string_data)
type = ''
count = 0
while string_data[count] != ':' do
type << string_data[count]
count+=1
end
type
end
def find_data(string_data)
data = ''
count = string_data.index(':') + 1
while string_data[count] != nil do
data << string_data[count]
count+=1
end
data
end
def set_data(type, data)
case type
when 'boolean' then @data = (data == 'true')
when 'string' then @data = data
when 'number' then @data = chack_type_of_number(data)
when 'symbol' then @data = data.to_sym
end
end
def chack_type_of_number(data)
if data.include? "." then data.to_f
else data.to_i
end
end
end
end

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

FFFFFFFFFFFFFFFFFFF.....F..FF..F..F..F..F..F

Failures:

  1) RBFS Directory can add a file
     Failure/Error: subject(:directory) { RBFS::Directory.new }
     NameError:
       uninitialized constant RBFS::Directory
     # /tmp/d20141111-26053-niiw6v/spec.rb:3:in `block (3 levels) in <top (required)>'
     # /tmp/d20141111-26053-niiw6v/spec.rb:144: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 can create empty directory
     Failure/Error: subject(:directory) { RBFS::Directory.new }
     NameError:
       uninitialized constant RBFS::Directory
     # /tmp/d20141111-26053-niiw6v/spec.rb:3:in `block (3 levels) in <top (required)>'
     # /tmp/d20141111-26053-niiw6v/spec.rb:150: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)>'

  3) RBFS Directory can add a directory
     Failure/Error: subdirectory = RBFS::Directory.new
     NameError:
       uninitialized constant RBFS::Directory
     # /tmp/d20141111-26053-niiw6v/spec.rb:157: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)>'

  4) RBFS Directory without files returns empty hash for the files
     Failure/Error: subject(:directory) { RBFS::Directory.new }
     NameError:
       uninitialized constant RBFS::Directory
     # /tmp/d20141111-26053-niiw6v/spec.rb:3:in `block (3 levels) in <top (required)>'
     # /tmp/d20141111-26053-niiw6v/spec.rb:7:in `block (4 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 without files can be serialized
     Failure/Error: subject(:directory) { RBFS::Directory.new }
     NameError:
       uninitialized constant RBFS::Directory
     # /tmp/d20141111-26053-niiw6v/spec.rb:3:in `block (3 levels) in <top (required)>'
     # /tmp/d20141111-26053-niiw6v/spec.rb:11:in `block (4 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 without directories returns empty hash for the directories
     Failure/Error: subject(:directory) { RBFS::Directory.new }
     NameError:
       uninitialized constant RBFS::Directory
     # /tmp/d20141111-26053-niiw6v/spec.rb:3:in `block (3 levels) in <top (required)>'
     # /tmp/d20141111-26053-niiw6v/spec.rb:17:in `block (4 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 Directory with files and directories returns correct file hash
     Failure/Error: subject(:directory) { RBFS::Directory.new }
     NameError:
       uninitialized constant RBFS::Directory
     # /tmp/d20141111-26053-niiw6v/spec.rb:3:in `block (3 levels) in <top (required)>'
     # /tmp/d20141111-26053-niiw6v/spec.rb:27:in `block (4 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 Directory with files and directories returns correct directory hash
     Failure/Error: subject(:directory) { RBFS::Directory.new }
     NameError:
       uninitialized constant RBFS::Directory
     # /tmp/d20141111-26053-niiw6v/spec.rb:3:in `block (3 levels) in <top (required)>'
     # /tmp/d20141111-26053-niiw6v/spec.rb:27:in `block (4 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 Directory serialization #serialize can serialize
     Failure/Error: subject(:directory) { RBFS::Directory.new }
     NameError:
       uninitialized constant RBFS::Directory
     # /tmp/d20141111-26053-niiw6v/spec.rb:3:in `block (3 levels) in <top (required)>'
     # /tmp/d20141111-26053-niiw6v/spec.rb:77: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 Directory serialization #serialize can serialize multiple directories recursively
     Failure/Error: subject(:directory) { RBFS::Directory.new }
     NameError:
       uninitialized constant RBFS::Directory
     # /tmp/d20141111-26053-niiw6v/spec.rb:3:in `block (3 levels) in <top (required)>'
     # /tmp/d20141111-26053-niiw6v/spec.rb:85: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 Directory serialization ::parse can parse empty directories
     Failure/Error: parsed_directory = RBFS::Directory.parse('0:0:')
     NameError:
       uninitialized constant RBFS::Directory
     # /tmp/d20141111-26053-niiw6v/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)>'

  12) RBFS Directory serialization ::parse can parse directories with files
     Failure/Error: parsed_directory = RBFS::Directory.parse(simple_serialized_string)
     NameError:
       uninitialized constant RBFS::Directory
     # /tmp/d20141111-26053-niiw6v/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)>'

  13) 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:')
     NameError:
       uninitialized constant RBFS::Directory
     # /tmp/d20141111-26053-niiw6v/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)>'

  14) RBFS Directory serialization ::parse can parse directories recursively
     Failure/Error: parsed_directory = RBFS::Directory.parse(recursive_serialized_string)
     NameError:
       uninitialized constant RBFS::Directory
     # /tmp/d20141111-26053-niiw6v/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)>'

  15) RBFS Directory #[] can walk a single directory
     Failure/Error: subject(:directory) { RBFS::Directory.new }
     NameError:
       uninitialized constant RBFS::Directory
     # /tmp/d20141111-26053-niiw6v/spec.rb:3:in `block (3 levels) in <top (required)>'
     # /tmp/d20141111-26053-niiw6v/spec.rb:171:in `block (4 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)>'

  16) RBFS Directory #[] can walk multiple directories
     Failure/Error: subject(:directory) { RBFS::Directory.new }
     NameError:
       uninitialized constant RBFS::Directory
     # /tmp/d20141111-26053-niiw6v/spec.rb:3:in `block (3 levels) in <top (required)>'
     # /tmp/d20141111-26053-niiw6v/spec.rb:171:in `block (4 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)>'

  17) RBFS Directory #[] can get files
     Failure/Error: subject(:directory) { RBFS::Directory.new }
     NameError:
       uninitialized constant RBFS::Directory
     # /tmp/d20141111-26053-niiw6v/spec.rb:3:in `block (3 levels) in <top (required)>'
     # /tmp/d20141111-26053-niiw6v/spec.rb:171:in `block (4 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)>'

  18) RBFS Directory #[] returns nil if directory or file doesnt exist
     Failure/Error: subject(:directory) { RBFS::Directory.new }
     NameError:
       uninitialized constant RBFS::Directory
     # /tmp/d20141111-26053-niiw6v/spec.rb:3:in `block (3 levels) in <top (required)>'
     # /tmp/d20141111-26053-niiw6v/spec.rb:171:in `block (4 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)>'

  19) RBFS Directory #[] is case-sensitive
     Failure/Error: subject(:directory) { RBFS::Directory.new }
     NameError:
       uninitialized constant RBFS::Directory
     # /tmp/d20141111-26053-niiw6v/spec.rb:3:in `block (3 levels) in <top (required)>'
     # /tmp/d20141111-26053-niiw6v/spec.rb:171:in `block (4 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)>'

  20) RBFS File data type nil can be parsed
     Failure/Error: expect(file.data     ).to eq nil
     NoMethodError:
       undefined method `data' for nil:NilClass
     # /tmp/d20141111-26053-niiw6v/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)>'

  21) RBFS File data type string can be parsed
     Failure/Error: expect(file.data     ).to eq 'Hey there'
     NoMethodError:
       undefined method `data' for "Hey there":String
     # /tmp/d20141111-26053-niiw6v/spec.rb:250: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)>'

  22) RBFS File data type string can parse a string with colons
     Failure/Error: expect(file.data     ).to eq 'Hay :)'
     NoMethodError:
       undefined method `data' for "Hay :)":String
     # /tmp/d20141111-26053-niiw6v/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)>'

  23) RBFS File data type symbol can be parsed
     Failure/Error: expect(file.data     ).to eq :hello
     NoMethodError:
       undefined method `data' for :hello:Symbol
     # /tmp/d20141111-26053-niiw6v/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)>'

  24) RBFS File data type number can be parsed
     Failure/Error: expect(file.data     ).to eq 1234
     NoMethodError:
       undefined method `data' for 1234:Fixnum
     # /tmp/d20141111-26053-niiw6v/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)>'

  25) RBFS File data type float number can be parsed
     Failure/Error: expect(file.data     ).to eq 3.14
     NoMethodError:
       undefined method `data' for 3.14:Float
     # /tmp/d20141111-26053-niiw6v/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)>'

  26) RBFS File data type boolean true can be parsed
     Failure/Error: expect(file.data     ).to eq true
     NoMethodError:
       undefined method `data' for true:TrueClass
     # /tmp/d20141111-26053-niiw6v/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)>'

  27) RBFS File data type boolean false can be parsed
     Failure/Error: expect(file.data     ).to eq false
     NoMethodError:
       undefined method `data' for false:FalseClass
     # /tmp/d20141111-26053-niiw6v/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.03818 seconds
44 examples, 27 failures

Failed examples:

rspec /tmp/d20141111-26053-niiw6v/spec.rb:141 # RBFS Directory can add a file
rspec /tmp/d20141111-26053-niiw6v/spec.rb:149 # RBFS Directory can create empty directory
rspec /tmp/d20141111-26053-niiw6v/spec.rb:156 # RBFS Directory can add a directory
rspec /tmp/d20141111-26053-niiw6v/spec.rb:6 # RBFS Directory without files returns empty hash for the files
rspec /tmp/d20141111-26053-niiw6v/spec.rb:10 # RBFS Directory without files can be serialized
rspec /tmp/d20141111-26053-niiw6v/spec.rb:16 # RBFS Directory without directories returns empty hash for the directories
rspec /tmp/d20141111-26053-niiw6v/spec.rb:33 # RBFS Directory with files and directories returns correct file hash
rspec /tmp/d20141111-26053-niiw6v/spec.rb:37 # RBFS Directory with files and directories returns correct directory hash
rspec /tmp/d20141111-26053-niiw6v/spec.rb:76 # RBFS Directory serialization #serialize can serialize
rspec /tmp/d20141111-26053-niiw6v/spec.rb:84 # RBFS Directory serialization #serialize can serialize multiple directories recursively
rspec /tmp/d20141111-26053-niiw6v/spec.rb:100 # RBFS Directory serialization ::parse can parse empty directories
rspec /tmp/d20141111-26053-niiw6v/spec.rb:107 # RBFS Directory serialization ::parse can parse directories with files
rspec /tmp/d20141111-26053-niiw6v/spec.rb:116 # RBFS Directory serialization ::parse can parse directory trees without files
rspec /tmp/d20141111-26053-niiw6v/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively
rspec /tmp/d20141111-26053-niiw6v/spec.rb:177 # RBFS Directory #[] can walk a single directory
rspec /tmp/d20141111-26053-niiw6v/spec.rb:181 # RBFS Directory #[] can walk multiple directories
rspec /tmp/d20141111-26053-niiw6v/spec.rb:185 # RBFS Directory #[] can get files
rspec /tmp/d20141111-26053-niiw6v/spec.rb:189 # RBFS Directory #[] returns nil if directory or file doesnt exist
rspec /tmp/d20141111-26053-niiw6v/spec.rb:193 # RBFS Directory #[] is case-sensitive
rspec /tmp/d20141111-26053-niiw6v/spec.rb:229 # RBFS File data type nil can be parsed
rspec /tmp/d20141111-26053-niiw6v/spec.rb:247 # RBFS File data type string can be parsed
rspec /tmp/d20141111-26053-niiw6v/spec.rb:254 # RBFS File data type string can parse a string with colons
rspec /tmp/d20141111-26053-niiw6v/spec.rb:273 # RBFS File data type symbol can be parsed
rspec /tmp/d20141111-26053-niiw6v/spec.rb:292 # RBFS File data type number can be parsed
rspec /tmp/d20141111-26053-niiw6v/spec.rb:311 # RBFS File data type float number can be parsed
rspec /tmp/d20141111-26053-niiw6v/spec.rb:331 # RBFS File data type boolean true can be parsed
rspec /tmp/d20141111-26053-niiw6v/spec.rb:350 # RBFS File data type boolean false can be parsed

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

Калин обнови решението на 10.11.2014 14:07 (преди над 9 години)

+module RBFS
+ class File
+ attr_accessor :data
+
+ def initialize(data = nil)
+ @data = data
+ end
+
+ def data_type
+ return :boolean if @data.class == TrueClass
+ return :boolean if @data.class == FalseClass
+ return :string if @data.class == String
+ return :number if @data.class == Fixnum
+ return :number if @data.class == Float
+ return :number if @data.class == Bignum
+ return :symbol if @data.class == Symbol
+ return :nil if @data.class == NilClass
+ end
+
+ def serialize
+ data_type.to_s + ':' + data.to_s
+ end
+
+ def self.parse(string_data)
+ a = File.new
+ data = a.find_data(string_data)
+ type = a.find_type(string_data)
+ a.set_data(type, data)
+ end
+
+ def find_type(string_data)
+ type = ''
+ count = 0
+ while string_data[count] != ':' do
+ type << string_data[count]
+ count+=1
+ end
+ type
+ end
+
+ def find_data(string_data)
+ data = ''
+ count = string_data.index(':') + 1
+ while string_data[count] != nil do
+ data << string_data[count]
+ count+=1
+ end
+ data
+ end
+
+ def set_data(type, data)
+ case type
+ when 'boolean' then @data = (data == 'true')
+ when 'string' then @data = data
+ when 'number' then @data = chack_type_of_number(data)
+ when 'symbol' then @data = data.to_sym
+ end
+ end
+
+ def chack_type_of_number(data)
+ if data.include? "." then data.to_f
+ else data.to_i
+ end
+ end
+
+ end
+end