A BinData::BasePrimitive object is a container for a value that has a particular binary representation. A value corresponds to a primitive type such as as integer, float or string. Only one value can be contained by this object. This value can be read from or written to an IO stream.
require 'bindata' obj = BinData::Uint8.new(:initial_value => 42) obj #=> 42 obj.assign(5) obj #=> 5 obj.clear obj #=> 42 obj = BinData::Uint8.new(:value => 42) obj #=> 42 obj.assign(5) obj #=> 42 obj = BinData::Uint8.new(:check_value => 3) obj.read("\005") #=> BinData::ValidityError: value is '5' but expected '3' obj = BinData::Uint8.new(:check_value => lambda { value < 5 }) obj.read("\007") #=> BinData::ValidityError: value not as expected
Parameters may be provided at initialisation to control the behaviour of an object. These params include those for BinData::Base as well as:
This is the initial value to use before one is either read or explicitly set with value=.
The object will always have this value. Calls to value= are ignored when using this param. While reading, value will return the value of the data read from the IO, not the result of the :value param.
Raise an error unless the value read or assigned meets this criteria. The variable value is made available to any lambda assigned to this parameter. A boolean return indicates success or failure. Any other return is compared to the value just read in.
Equavalent to :assert and :value.
# File lib/bindata/alignment.rb, line 71 def BasePrimitive.bit_aligned include BitAligned end
# File lib/bindata/base_primitive.rb, line 119 def <=>(other) snapshot <=> other end
# File lib/bindata/base_primitive.rb, line 81 def assign(val) raise ArgumentError, "can't set a nil value for #{debug_name}" if val.nil? raw_val = val.respond_to?(:snapshot) ? val.snapshot : val @value = begin raw_val.dup rescue TypeError # can't dup Fixnums raw_val end end
# File lib/bindata/trace.rb, line 57 def do_read_with_hook(io) do_read_without_hook(io) trace_value end
# File lib/bindata/base_primitive.rb, line 123 def eql?(other) # double dispatch other.eql?(snapshot) end
# File lib/bindata/base_primitive.rb, line 128 def hash snapshot.hash end
# File lib/bindata/base_primitive.rb, line 73 def initialize_instance @value = nil end
# File lib/bindata/base_primitive.rb, line 93 def snapshot _value end
Generated with the Darkfish Rdoc Generator 2.