Class: Bytes
Constant Summary
Constants included
from BytesHelper
BytesHelper::HEX_RE
Class Method Summary
collapse
Instance Method Summary
collapse
bin_to_hex, hex_to_bin, is_hex?
Constructor Details
#initialize(bin = String.new) ⇒ Bytes
Returns a new instance of Bytes.
64
65
66
67
68
69
|
# File 'lib/bytes.rb', line 64
def initialize( bin=String.new )
raise ArgumentError, "Bytes.new - BINARY/ASCII-8BIT encoding expected; got: #{bin.encoding} for string >#{bin}<" if bin.encoding != Encoding::ASCII_8BIT
@bin = bin
end
|
Class Method Details
.convert(arg) ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/bytes.rb', line 92
def self.convert( arg )
if arg.is_a?( Bytes )
arg
elsif arg.is_a?( String )
if arg.encoding == Encoding::ASCII_8BIT
Bytes.wrap( arg )
else
Bytes.from_hex( arg )
end
else
raise ArgumentError, "Bytes() expected String; got #{arg.class.name}"
end
end
|
.from_hex(hex) ⇒ Object
58
|
# File 'lib/bytes.rb', line 58
def self.from_hex( hex ) new( hex_to_bin( hex ) ); end
|
.wrap(bin) ⇒ Object
“semantic” constructor for wrapping (binary) strings e.g. same as Bytes.new(bin)
61
|
# File 'lib/bytes.rb', line 61
def self.wrap( bin ) new( bin ); end
|
Instance Method Details
#==(other) ⇒ Object
86
87
88
|
# File 'lib/bytes.rb', line 86
def ==(other)
self.class == other.class && @bin == other.b
end
|
#b ⇒ Object
81
|
# File 'lib/bytes.rb', line 81
def b() @bin; end
|
#to_s ⇒ Object
Also known as:
to_hex
78
|
# File 'lib/bytes.rb', line 78
def to_s() Bytes.bin_to_hex( @bin ); end
|