Class: HrrRbSsh::DataType::String
- Inherits:
-
HrrRbSsh::DataType
- Object
- HrrRbSsh::DataType
- HrrRbSsh::DataType::String
- Defined in:
- lib/hrr_rb_ssh/data_type/string.rb
Overview
String provides methods to convert Ruby’s string value and binary string each other.
Class Method Summary collapse
-
.decode(io) ⇒ ::String
Convert binary string into Ruby’s string value.
-
.encode(arg) ⇒ ::String
Convert Ruby’s string value into binary string.
Class Method Details
.decode(io) ⇒ ::String
Convert binary string into Ruby’s string value.
28 29 30 31 |
# File 'lib/hrr_rb_ssh/data_type/string.rb', line 28 def self.decode io length = io.read(4).unpack("N")[0] io.read(length).unpack("a*")[0] end |
.encode(arg) ⇒ ::String
Convert Ruby’s string value into binary string.
14 15 16 17 18 19 20 21 22 |
# File 'lib/hrr_rb_ssh/data_type/string.rb', line 14 def self.encode arg unless arg.kind_of? ::String raise ArgumentError, "must be a kind of String, but got #{arg.inspect}" end if arg.length > 0xffff_ffff raise ArgumentError, "must be shorter than or equal to #{0xffff_ffff}, but got length #{arg.length}" end [arg.length, arg].pack("Na*") end |