Любомир обнови решението на 08.11.2014 18:08 (преди около 10 години)
+class String
+ def to_rbfs_type
+ if self == "true" then true
+ elsif self == "false" then false
+ elsif self.include? "." then self.to_f
+ else
+ self.to_i
+ end
+ end
+end
+
+
+module RBFS
+ class File
+ attr_reader :data_type
+
+ attr_reader :data
+
+ def serialize
+ "#{data_type}:#{data.to_s}"
+ end
+
+ def self.parse(string_data)
+ type_and_data=string_data.split(':', 2)
+ case type_and_data[0]
+ when "string" then File.new(type_and_data[1])
+ when "boolean" then File.new(type_and_data[1].to_rbfs_type)
+ when "number" then File.new(type_and_data[1].to_rbfs_type)
+ when "symbol" then File.new(type_and_data[1].to_sym)
+ when "nil" then File.new()
+ end
+ end
+
+ def data=(value)
+ @data = value
+ update_data_type
+ end
+
+ def update_data_type
+ case @data.class.to_s
+ when "String" then @data_type = :string
+ when "Symbol" then @data_type = :symbol
+ when "Fixnum" then @data_type = :number
+ when "Float" then @data_type = :number
+ when "NilClass" then @data_type = :nil
+ else @data_type = :boolean
+ end
+ end
+
+ def initialize (*args)
+ @data = args[0]
+ update_data_type
+ end
+ end
+
+ class Directory
+ attr_reader :files
+
+ attr_reader :directories
+
+ def file_serialize
+ res = @files.count.to_s + ":"
+ @files.each_pair do |name, value|
+ number_of_symbols = value.data.to_s.length + value.data_type.to_s.length + 1
+ res += "#{name}:#{number_of_symbols}:#{value.serialize}"
+ end
+ res
+ end
+
+ def serialize
+ temp1= ""
+ res = "" + file_serialize
+ directories.each_pair do |name, folder|
+ temp2 = ""
+ temp2 += folder.serialize
+ temp1 += name.to_s + ":" + temp2.length.to_s + ":" + temp2
+ end
+ res = res + directories.count.to_s + ":" + temp1
+ end
+
+ def initialize
+ @files = {}
+ @directories = {}
+ end
+
+ def add_directory(name, *directory)
+ @directories[name] = directory[0] unless name.to_s.include?(":")
+ end
+
+
+ def add_file(name, file)
+ @files[name] = file unless name.to_s.include?(":")
+ end
+
+ def [](name)
+ if @directories[name] then @directories[name]
+ elsif @files[name] then @files[name]
+ else nil
+ end
+ end
+ end
+end
--