Class: BitStruct::FloatField
- Defined in:
- lib/bit-struct/float-field.rb
Overview
Class for floats (single and double precision) in network order. Declared with BitStruct.float.
Instance Attribute Summary
Attributes inherited from Field
#default, #display_name, #format, #length, #name, #offset, #options
Class Method Summary collapse
-
.class_name ⇒ Object
Used in describe.
Instance Method Summary collapse
Methods inherited from Field
#class_name, default, #describe, #initialize, #inspect_in_object, #inspectable?
Constructor Details
This class inherits a constructor from BitStruct::Field
Class Method Details
.class_name ⇒ Object
Used in describe.
6 7 8 |
# File 'lib/bit-struct/float-field.rb', line 6 def self.class_name @class_name ||= "float" end |
Instance Method Details
#add_accessors_to(cl, attr = name) ⇒ Object
:nodoc:
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/bit-struct/float-field.rb', line 10 def add_accessors_to(cl, attr = name) # :nodoc: unless offset % 8 == 0 raise ArgumentError, "Bad offset, #{offset}, for #{self.class} #{name}." + " Must be multiple of 8." end unless length == 32 or length == 64 raise ArgumentError, "Bad length, #{length}, for #{self.class} #{name}." + " Must be 32 or 64." end offset_byte = offset / 8 length_byte = length / 8 last_byte = offset_byte + length_byte - 1 byte_range = offset_byte..last_byte endian = ([:endian] || ["endian"]).to_s case endian when "native" ctl = case length when 32; "f" when 64; "d" end when "little" ctl = case length when 32; "e" when 64; "E" end when "network", "big", "" ctl = case length when 32; "g" when 64; "G" end else raise ArgumentError, "Unrecognized endian option: #{endian.inspect}" end cl.class_eval do define_method attr do || self[byte_range].unpack(ctl).first end define_method "#{attr}=" do |val| self[byte_range] = [val].pack(ctl) end end end |