Module: Packable::Extensions::Float::ClassMethods

Defined in:
lib/packable/extensions/float.rb

Overview

:nodoc:

Constant Summary collapse

ENDIAN_TO_FORMAT =
Hash.new{|h, endian| raise ArgumentError, "Endian #{endian} is not valid. It must be one of #{h.keys.join(', ')}."}.
merge!(:big => "G", :network => "G", :little => "E", :native => "D").freeze
FORMAT_TO_SINGLE_PRECISION =
{'G' => 'g', 'E' => 'e', 'D' => 'f'}.freeze
PRECISION =
Hash.new{|h, precision| raise ArgumentError, "Precision #{precision} is not valid. It must be one of #{h.keys.join(', ')}."}.
merge!(:single => 4, :double => 8).freeze

Instance Method Summary collapse

Instance Method Details

#pack_option_to_format(options) ⇒ Object



30
31
32
33
34
# File 'lib/packable/extensions/float.rb', line 30

def pack_option_to_format(options)
  format = ENDIAN_TO_FORMAT[options[:endian]]
  format = FORMAT_TO_SINGLE_PRECISION[format] if options[:precision] == :single
  format
end

#read_packed(io, options) ⇒ Object



36
37
38
39
# File 'lib/packable/extensions/float.rb', line 36

def read_packed(io, options)
  s = io.read_exactly(PRECISION[options[:precision]])
  s && s.unpack(pack_option_to_format(options)).first
end