Class: HrrRbSsh::DataType::Boolean

Inherits:
HrrRbSsh::DataType show all
Defined in:
lib/hrr_rb_ssh/data_type/boolean.rb

Overview

Boolean provides methods to convert boolean value and 8-bit unsigned binary string each other.

Class Method Summary collapse

Class Method Details

.decode(io) ⇒ ::Boolean

Convert 8-bit unsigned binary into boolean value.

Parameters:

  • io (::IO)

    IO instance that has buffer to be read

Returns:

  • (::Boolean)

    converted boolean value



28
29
30
31
32
33
34
# File 'lib/hrr_rb_ssh/data_type/boolean.rb', line 28

def self.decode io
  if 0 == io.read(1).unpack("C")[0]
    false
  else
    true
  end
end

.encode(arg) ⇒ ::String

Convert boolean value into 8-bit unsigned binary string.

Parameters:

  • arg (::Boolean)

    boolean value to be converted

Returns:

  • (::String)

    converted 8-bit unsigned binary string

Raises:

  • (::ArgumentError)

    when arg is not true nor false



13
14
15
16
17
18
19
20
21
22
# File 'lib/hrr_rb_ssh/data_type/boolean.rb', line 13

def self.encode arg
  case arg
  when false
    [0].pack("C")
  when true
    [1].pack("C")
  else
    raise ArgumentError, "must be #{true} or #{false}, but got #{arg.inspect}"
  end
end