Module: BinData::Float
- Defined in:
- lib/bindata/float.rb
Overview
Provides a number of classes that contain a floating point number. The float is defined by endian, and precision.
Class Method Summary collapse
-
.create_float_methods(klass, single_precision, endian) ⇒ Object
:nodoc: all.
- .create_read_code(single_precision, endian) ⇒ Object
- .create_to_s_code(single_precision, endian) ⇒ Object
- .define_methods(klass, single_precision, read, to_s) ⇒ Object
Class Method Details
.create_float_methods(klass, single_precision, endian) ⇒ Object
:nodoc: all
8 9 10 11 12 13 |
# File 'lib/bindata/float.rb', line 8 def self.create_float_methods(klass, single_precision, endian) read = create_read_code(single_precision, endian) to_s = create_to_s_code(single_precision, endian) define_methods(klass, single_precision, read, to_s) end |
.create_read_code(single_precision, endian) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/bindata/float.rb', line 15 def self.create_read_code(single_precision, endian) if single_precision unpack = (endian == :little) ? 'e' : 'g' nbytes = 4 else # double_precision unpack = (endian == :little) ? 'E' : 'G' nbytes = 8 end "readbytes(io,#{nbytes}).unpack('#{unpack}').at(0)" end |
.create_to_s_code(single_precision, endian) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/bindata/float.rb', line 27 def self.create_to_s_code(single_precision, endian) if single_precision pack = (endian == :little) ? 'e' : 'g' else # double_precision pack = (endian == :little) ? 'E' : 'G' end "[val].pack('#{pack}')" end |
.define_methods(klass, single_precision, read, to_s) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/bindata/float.rb', line 37 def self.define_methods(klass, single_precision, read, to_s) nbytes = single_precision ? 4 : 8 # define methods in the given class klass.module_eval <<-END def _num_bytes(ignored) #{nbytes} end #--------------- private def sensible_default 0.0 end def val_to_str(val) #{to_s} end def read_val(io) #{read} end END end |