1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#!/usr/bin/env ruby
# This example demonstrates encoding and decoding a Ruby object.
# Use the current repo if run from the examples directory.
ox_dir = File.dirname(File.dirname(File.expand_path(__FILE__)))
$LOAD_PATH << File.join(ox_dir, 'ext')
$LOAD_PATH << File.join(ox_dir, 'lib')
require 'ox'
# Define a class that will be used for instances that are encoded and decoded.
class Classy
def initialize(a, b)
@a = a
@b = b
end
def to_s
"Classy a: #{@a}, b: #{@b}"
end
end
obj = Classy.new(23, ['abc', { x: true }])
doc = Ox.dump(obj, mode: :object)
# The encoded format is not important other and should ot be generated by
# hand. It is of interest only for the curious.
puts "encoded object:\n#{doc}"
# Now convert back to a Ruby object.
obj2 = Ox.load(doc, mode: :object)
# Looks the same, print it out to check.
puts "decoded object: #{obj2}"
|