examples/list.rb in bindata-1.3.1 vs examples/list.rb in bindata-1.4.0
- old
+ new
@@ -1,10 +1,10 @@
require 'bindata'
# An example of a recursively defined data format.
#
-# This format is used to describe atoms and lists.
+# This example format describes atoms and lists.
# It is recursive because lists can contain other lists.
#
# Atoms - contain a single integer
# Lists - contain a mixture of atoms and lists
#
@@ -47,14 +47,13 @@
# new type 'Term' that is a thin container around the tag plus the
# type (atom or list).
#
# The declaration then becomes:
#
-# class Term < BinData::Record; end # forward declaration
+# class Term < BinData::Record; end # forward declaration
#
-# class Atom < BinData::Wrapper
-# int32be
+# class Atom < BinData::Int32be
# end
#
# class List < BinData::Record
# int32be :num, :value => lambda { vals.length }
# array :vals, :type => :term, :initial_length => :num
@@ -69,13 +68,11 @@
# end
class Term < BinData::Record; end # Forward declaration
-class Atom < BinData::Wrapper
- int32be
-
+class Atom < BinData::Int32be
def decode
snapshot
end
def self.encode(val)
@@ -115,10 +112,13 @@
end
end
end
+puts "A single Atom"
p Term.encode(4)
p Term.encode(4).decode
puts
+
+puts "A nested List"
p Term.encode([1, [2, 3], 4])
p Term.encode([1, [2, 3], 4]).decode