Дамян обнови решението на 06.11.2014 14:20 (преди около 10 години)
+module RBFS
+ class File
+ attr_accessor :data
+
+ def initialize(file = nil)
+ @data = file
+ end
+
+ def data_type
+ return :number if @data.is_a? Numeric
+ return :nil if @data.class == NilClass
+ return :string if @data.class == String
+ return :boolean if @data.class == TrueClass || @data.class == FalseClass
+ return :symbol if @data.class == Symbol
+ end
+
+ def serialize
+ "#{data_type}:#{data}"
+ end
+
+ def self.parse(serialized_string)
+ file_type = serialized_string.split(':')[0]
+ file_data = serialized_string.split(':')[1]
+ return RBFS::File.new file_data.to_num if file_type == 'number'
+ return RBFS::File.new if file_type == 'nil'
+ return RBFS::File.new file_data if file_type == 'string'
+ return RBFS::File.new file_data.to_sym if file_type == 'symbol'
+ return RBFS::File.new file_data == 'true' if file_type == 'boolean'
+ end
+ end
+
+ class Directory
+ attr_accessor :files, :directories
+
+ def initialize
+ @files = {}
+ @directories = {}
+ end
+
+ def add_file(name, file)
+ @files[name] = file
+ end
+
+ def add_directory(name, directory = RBFS::Directory.new)
+ @directories[name] = directory
+ end
+
+ def[](name)
+ if @directories[name].is_a? Directory
+ @directories[name]
+ else
+ @files[name]
+ end
+ end
+
+ def serialize
+ "#{serialized_files}#{serialized_directories}"
+ end
+
+ def serialized_files
+ result = ["#{@files.size}:"]
+ @files.each do |name, file|
+ result << "#{name}:#{file.serialize.size}:#{file.serialize}"
+ end
+
+ result.join ''
+ end
+
+ def serialized_directories
+ result = ["#{@directories.size}:"]
+ @directories.each do |name, directory|
+ result << "#{name}:#{directory.serialize.size}:#{directory.serialize}"
+ end
+
+ result.join ''
+ end
+ end
+end
+
+class String
+ def to_num
+ if include? '.'
+ to_f
+ else
+ to_i
+ end
+ end
+end
Все още не съм направил RBFS::Directory.parse :(, но работя усилено по въпроса :)