Class: MemoryIO::Types::Basic::Number

Inherits:
Object
  • Object
show all
Defined in:
lib/memory_io/types/basic/number.rb

Overview

Register numbers to MemoryIO::Types.

All types registerd by this class are assumed as *little endian*.

This class registered (un)signed {8, 16, 32, 64)-bit integers and IEEE-754 floating numbers.

Instance Method Summary collapse

Constructor Details

#initialize(bytes, signed, pack_str) ⇒ Number

Returns a new instance of Number.

Parameters:

  • bytes (Integer)

    Bytes.

  • signed (Boolean)

    Signed or unsigned.

  • pack_str (String)

    The indicator to be passed to Array#pack and String#unpack.


19
20
21
22
23
# File 'lib/memory_io/types/basic/number.rb', line 19

def initialize(bytes, signed, pack_str)
  @bytes = bytes
  @signed = signed
  @pack_str = pack_str
end

Instance Method Details

#read(stream) ⇒ Integer

Returns:

  • (Integer)

26
27
28
# File 'lib/memory_io/types/basic/number.rb', line 26

def read(stream)
  unpack(stream.read(@bytes))
end

#write(stream, val) ⇒ Object

Parameters:

  • val (Integer)

31
32
33
# File 'lib/memory_io/types/basic/number.rb', line 31

def write(stream, val)
  stream.write(pack(val))
end