Решение на Трета задача от Любослава Димитрова

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

Към профила на Любослава Димитрова

Резултати

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

Код

class String
def to_bool
case self
when "true" then true
when "false" then false
end
end
def string_conversion(type)
case type
when "nil" then return nil
when "symbol" then return self.to_sym
when "string" then return self
when "boolean" then return self.to_bool
end
return self.to_f if type == "number" and self.include? "."
return self.to_i if type == "number" and !self.include? "."
end
end
module RBFS
class File
def initialize(object = nil)
@data = object
@data_type = case @data
when NilClass then :nil
when String then :string
when Fixnum, Float then :number
when Symbol then :symbol
when TrueClass, FalseClass then :boolean
end
end
attr_reader :data_type
attr_accessor :data
def serialize
"#{self.data_type.to_s}:#{@data.to_s}"
end
def self.parse(string_data)
parsed = File.new
data_type = string_data.split(":")[0]
data = string_data.split(":")[1].to_s
parsed.data = data.string_conversion(data_type)
parsed
end
end
class Directory
def initialize
@directory = {}
end
attr_reader :files, :directories
def add_file(name, file)
if name.include? ":"
"name cannot contain a colon"
else @directory[name] = file
end
@files = @directory.select {|key, object| object.class == File}
end
def add_directory(name, directory = {})
if name.include? ":"
"name cannot contain a colon"
elsif directory == {}
@directory[name] = Directory.new
else @directory[name] = directory
end
@directories = @directory.select{|key, object| object.class == Directory}
end
def [](name)
matches = @directory.select{|key, hash| key == name}
matching_dirs = matches.select{|key, hash| hash[key].class == Directory}
case matches.length
when 0 then nil
when 1 then matches[name]
else matching_dirs[name]
end
end
end
end

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

...FFF..FFFFFF..F........FFFFFFFFFFFFFFFFFFF

Failures:

  1) RBFS Directory without files returns empty hash for the files
     Failure/Error: expect(directory.files).to eq({})
       
       expected: {}
            got: nil
       
       (compared using ==)
     # /tmp/d20141111-26053-1jobdxu/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)>'

  2) RBFS Directory without files can be serialized
     Failure/Error: expect(directory.serialize).to eq '0:0:'
     NoMethodError:
       undefined method `serialize' for #<RBFS::Directory:0xb9700fe4 @directory={}>
     # /tmp/d20141111-26053-1jobdxu/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)>'

  3) RBFS Directory without directories returns empty hash for the directories
     Failure/Error: expect(directory.directories).to eq({})
       
       expected: {}
            got: nil
       
       (compared using ==)
     # /tmp/d20141111-26053-1jobdxu/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)>'

  4) RBFS Directory serialization #serialize can serialize
     Failure/Error: expect(directory.serialize).to eq simple_serialized_string
     NoMethodError:
       undefined method `serialize' for #<RBFS::Directory:0xb96fcb60>
     # /tmp/d20141111-26053-1jobdxu/spec.rb:81: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 Directory serialization #serialize can serialize multiple directories recursively
     Failure/Error: expect(directory.serialize).to eq recursive_serialized_string
     NoMethodError:
       undefined method `serialize' for #<RBFS::Directory:0xb96fbc74>
     # /tmp/d20141111-26053-1jobdxu/spec.rb:95: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 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-1jobdxu/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)>'

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

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

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

  10) RBFS Directory #[] can get files
     Failure/Error: expect(directory['home']['user']['ruby']['file']).to eq file
     NoMethodError:
       undefined method `[]' for #<RBFS::File:0xb96f7b24 @data="hi", @data_type=:string>
     # /tmp/d20141111-26053-1jobdxu/solution.rb:82:in `block in []'
     # /tmp/d20141111-26053-1jobdxu/solution.rb:82:in `select'
     # /tmp/d20141111-26053-1jobdxu/solution.rb:82:in `[]'
     # /tmp/d20141111-26053-1jobdxu/spec.rb:186: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)>'

  11) RBFS File data type string can be detected
     Failure/Error: expect(file.data_type).to eq :string
       
       expected: :string
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:string
       +:nil
     # /tmp/d20141111-26053-1jobdxu/spec.rb:240: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 File data type string can be serialized
     Failure/Error: expect(file.serialize).to eq 'string:Hi'
       
       expected: "string:Hi"
            got: "nil:Hi"
       
       (compared using ==)
     # /tmp/d20141111-26053-1jobdxu/spec.rb:244: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 File data type string can be parsed
     Failure/Error: expect(file.data_type).to eq :string
       
       expected: :string
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:string
       +:nil
     # /tmp/d20141111-26053-1jobdxu/spec.rb:251: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 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-1jobdxu/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)>'

  15) RBFS File data type symbol can be detected
     Failure/Error: expect(file.data_type).to eq :symbol
       
       expected: :symbol
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:symbol
       +:nil
     # /tmp/d20141111-26053-1jobdxu/spec.rb:266: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)>'

  16) RBFS File data type symbol can be serialized
     Failure/Error: expect(file.serialize).to eq 'symbol:yo'
       
       expected: "symbol:yo"
            got: "nil:yo"
       
       (compared using ==)
     # /tmp/d20141111-26053-1jobdxu/spec.rb:270: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)>'

  17) RBFS File data type symbol can be parsed
     Failure/Error: expect(file.data_type).to eq :symbol
       
       expected: :symbol
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:symbol
       +:nil
     # /tmp/d20141111-26053-1jobdxu/spec.rb:277: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)>'

  18) RBFS File data type number can be detected
     Failure/Error: expect(file.data_type).to eq :number
       
       expected: :number
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:number
       +:nil
     # /tmp/d20141111-26053-1jobdxu/spec.rb:285: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)>'

  19) RBFS File data type number can be serialized
     Failure/Error: expect(file.serialize).to eq 'number:666'
       
       expected: "number:666"
            got: "nil:666"
       
       (compared using ==)
     # /tmp/d20141111-26053-1jobdxu/spec.rb:289: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)>'

  20) RBFS File data type number can be parsed
     Failure/Error: expect(file.data_type).to eq :number
       
       expected: :number
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:number
       +:nil
     # /tmp/d20141111-26053-1jobdxu/spec.rb:296: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 float number can be detected
     Failure/Error: expect(file.data_type).to eq :number
       
       expected: :number
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:number
       +:nil
     # /tmp/d20141111-26053-1jobdxu/spec.rb:304: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 float number can be serialized
     Failure/Error: expect(file.serialize).to eq 'number:666.6'
       
       expected: "number:666.6"
            got: "nil:666.6"
       
       (compared using ==)
     # /tmp/d20141111-26053-1jobdxu/spec.rb:308: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 float number can be parsed
     Failure/Error: expect(file.data_type).to eq :number
       
       expected: :number
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:number
       +:nil
     # /tmp/d20141111-26053-1jobdxu/spec.rb:315: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 boolean true can be detected
     Failure/Error: expect(file.data_type).to eq :boolean
       
       expected: :boolean
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:boolean
       +:nil
     # /tmp/d20141111-26053-1jobdxu/spec.rb:323: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)>'

  25) RBFS File data type boolean true can be serialized
     Failure/Error: expect(file.serialize).to eq 'boolean:true'
       
       expected: "boolean:true"
            got: "nil:true"
       
       (compared using ==)
     # /tmp/d20141111-26053-1jobdxu/spec.rb:328: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)>'

  26) RBFS File data type boolean true can be parsed
     Failure/Error: expect(file.data_type).to eq :boolean
       
       expected: :boolean
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:boolean
       +:nil
     # /tmp/d20141111-26053-1jobdxu/spec.rb:335: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 detected
     Failure/Error: expect(file.data_type).to eq :boolean
       
       expected: :boolean
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:boolean
       +:nil
     # /tmp/d20141111-26053-1jobdxu/spec.rb:342: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)>'

  28) RBFS File data type boolean false can be serialized
     Failure/Error: expect(file.serialize).to eq 'boolean:false'
       
       expected: "boolean:false"
            got: "nil:false"
       
       (compared using ==)
     # /tmp/d20141111-26053-1jobdxu/spec.rb:347: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)>'

  29) RBFS File data type boolean false can be parsed
     Failure/Error: expect(file.data_type).to eq :boolean
       
       expected: :boolean
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:boolean
       +:nil
     # /tmp/d20141111-26053-1jobdxu/spec.rb:354: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.05125 seconds
44 examples, 29 failures

Failed examples:

rspec /tmp/d20141111-26053-1jobdxu/spec.rb:6 # RBFS Directory without files returns empty hash for the files
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:10 # RBFS Directory without files can be serialized
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:16 # RBFS Directory without directories returns empty hash for the directories
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:76 # RBFS Directory serialization #serialize can serialize
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:84 # RBFS Directory serialization #serialize can serialize multiple directories recursively
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:100 # RBFS Directory serialization ::parse can parse empty directories
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:107 # RBFS Directory serialization ::parse can parse directories with files
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:116 # RBFS Directory serialization ::parse can parse directory trees without files
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:185 # RBFS Directory #[] can get files
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:239 # RBFS File data type string can be detected
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:243 # RBFS File data type string can be serialized
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:247 # RBFS File data type string can be parsed
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:254 # RBFS File data type string can parse a string with colons
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:265 # RBFS File data type symbol can be detected
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:269 # RBFS File data type symbol can be serialized
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:273 # RBFS File data type symbol can be parsed
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:284 # RBFS File data type number can be detected
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:288 # RBFS File data type number can be serialized
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:292 # RBFS File data type number can be parsed
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:303 # RBFS File data type float number can be detected
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:307 # RBFS File data type float number can be serialized
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:311 # RBFS File data type float number can be parsed
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:321 # RBFS File data type boolean true can be detected
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:326 # RBFS File data type boolean true can be serialized
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:331 # RBFS File data type boolean true can be parsed
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:340 # RBFS File data type boolean false can be detected
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:345 # RBFS File data type boolean false can be serialized
rspec /tmp/d20141111-26053-1jobdxu/spec.rb:350 # RBFS File data type boolean false can be parsed

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

Любослава обнови решението на 10.11.2014 14:15 (преди над 9 години)

+class String
+
+ def to_bool
+ case self
+ when "true" then true
+ when "false" then false
+ end
+ end
+
+ def string_conversion(type)
+ case type
+ when "nil" then return nil
+ when "symbol" then return self.to_sym
+ when "string" then return self
+ when "boolean" then return self.to_bool
+ end
+ return self.to_f if type == "number" and self.include? "."
+ return self.to_i if type == "number" and !self.include? "."
+ end
+
+end
+
+module RBFS
+
+ class File
+
+ def initialize(object = nil)
+ @data = object
+ @data_type = case @data
+ when NilClass then :nil
+ when String then :string
+ when Fixnum, Float then :number
+ when Symbol then :symbol
+ when TrueClass, FalseClass then :boolean
+ end
+ end
+
+ attr_reader :data_type
+
+ attr_accessor :data
+
+ def serialize
+ "#{self.data_type.to_s}:#{@data.to_s}"
+ end
+
+ def self.parse(string_data)
+ parsed = File.new
+ data_type = string_data.split(":")[0]
+ data = string_data.split(":")[1].to_s
+ parsed.data = data.string_conversion(data_type)
+ parsed
+ end
+ end
+
+ class Directory
+
+ def initialize
+ @directory = {}
+ end
+
+ attr_reader :files, :directories
+
+ def add_file(name, file)
+ if name.include? ":"
+ "name cannot contain a colon"
+ else @directory[name] = file
+ end
+ @files = @directory.select {|key, object| object.class == File}
+ end
+
+ def add_directory(name, directory = {})
+ if name.include? ":"
+ "name cannot contain a colon"
+ elsif directory == {}
+ @directory[name] = Directory.new
+ else @directory[name] = directory
+ end
+ @directories = @directory.select{|key, object| object.class == Directory}
+ end
+
+ def [](name)
+ matches = @directory.select{|key, hash| key == name}
+ matching_dirs = matches.select{|key, hash| hash[key].class == Directory}
+ case matches.length
+ when 0 then nil
+ when 1 then matches[name]
+ else matching_dirs[name]
+ end
+ end
+ end
+end

Любослава обнови решението на 10.11.2014 14:17 (преди над 9 години)

class String
def to_bool
case self
- when "true" then true
- when "false" then false
+ when "true" then true
+ when "false" then false
end
end
def string_conversion(type)
case type
- when "nil" then return nil
- when "symbol" then return self.to_sym
- when "string" then return self
- when "boolean" then return self.to_bool
+ when "nil" then return nil
+ when "symbol" then return self.to_sym
+ when "string" then return self
+ when "boolean" then return self.to_bool
end
return self.to_f if type == "number" and self.include? "."
return self.to_i if type == "number" and !self.include? "."
end
-
end
module RBFS
class File
def initialize(object = nil)
@data = object
@data_type = case @data
when NilClass then :nil
when String then :string
when Fixnum, Float then :number
when Symbol then :symbol
when TrueClass, FalseClass then :boolean
end
end
attr_reader :data_type
attr_accessor :data
def serialize
"#{self.data_type.to_s}:#{@data.to_s}"
end
def self.parse(string_data)
parsed = File.new
data_type = string_data.split(":")[0]
data = string_data.split(":")[1].to_s
parsed.data = data.string_conversion(data_type)
parsed
end
end
class Directory
def initialize
@directory = {}
end
attr_reader :files, :directories
def add_file(name, file)
if name.include? ":"
"name cannot contain a colon"
else @directory[name] = file
end
@files = @directory.select {|key, object| object.class == File}
end
def add_directory(name, directory = {})
if name.include? ":"
"name cannot contain a colon"
elsif directory == {}
@directory[name] = Directory.new
else @directory[name] = directory
end
@directories = @directory.select{|key, object| object.class == Directory}
end
def [](name)
matches = @directory.select{|key, hash| key == name}
matching_dirs = matches.select{|key, hash| hash[key].class == Directory}
case matches.length
when 0 then nil
when 1 then matches[name]
else matching_dirs[name]
end
end
end
end

Любослава обнови решението на 10.11.2014 14:18 (преди над 9 години)

class String
def to_bool
case self
when "true" then true
when "false" then false
end
end
def string_conversion(type)
case type
when "nil" then return nil
when "symbol" then return self.to_sym
when "string" then return self
when "boolean" then return self.to_bool
end
return self.to_f if type == "number" and self.include? "."
return self.to_i if type == "number" and !self.include? "."
end
end
module RBFS
class File
def initialize(object = nil)
@data = object
@data_type = case @data
- when NilClass then :nil
- when String then :string
- when Fixnum, Float then :number
- when Symbol then :symbol
- when TrueClass, FalseClass then :boolean
- end
+ when NilClass then :nil
+ when String then :string
+ when Fixnum, Float then :number
+ when Symbol then :symbol
+ when TrueClass, FalseClass then :boolean
+ end
end
attr_reader :data_type
attr_accessor :data
def serialize
"#{self.data_type.to_s}:#{@data.to_s}"
end
def self.parse(string_data)
parsed = File.new
data_type = string_data.split(":")[0]
data = string_data.split(":")[1].to_s
parsed.data = data.string_conversion(data_type)
parsed
end
end
class Directory
def initialize
@directory = {}
end
attr_reader :files, :directories
def add_file(name, file)
if name.include? ":"
"name cannot contain a colon"
else @directory[name] = file
end
@files = @directory.select {|key, object| object.class == File}
end
def add_directory(name, directory = {})
if name.include? ":"
"name cannot contain a colon"
elsif directory == {}
@directory[name] = Directory.new
else @directory[name] = directory
end
@directories = @directory.select{|key, object| object.class == Directory}
end
def [](name)
matches = @directory.select{|key, hash| key == name}
matching_dirs = matches.select{|key, hash| hash[key].class == Directory}
case matches.length
when 0 then nil
when 1 then matches[name]
else matching_dirs[name]
end
end
end
end