Class: Takumi::Packet::Varint
- Inherits:
-
Object
- Object
- Takumi::Packet::Varint
- Defined in:
- lib/takumi/packet/varint.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(int) ⇒ Varint
constructor
A new instance of Varint.
- #to_s ⇒ Object
Constructor Details
#initialize(int) ⇒ Varint
Returns a new instance of Varint.
17 18 19 |
# File 'lib/takumi/packet/varint.rb', line 17 def initialize(int) @int = int end |
Class Method Details
.decode(source) ⇒ Object
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/takumi/packet/varint.rb', line 5 def decode(source) int = 0 source.chars.each.with_index(1) do |char, index| byte = char.ord int <<= 7 int += byte & 0b111_1111 return int, index if (byte & 0b1000_0000).zero? end end |
Instance Method Details
#to_s ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/takumi/packet/varint.rb', line 21 def to_s packet = "".encode(Encoding::BINARY) int = @int loop do bits = int & 0b111_1111 int = int >> 7 if int.zero? packet << bits.chr and break else packet << (bits | 0b1000_0000).chr end end packet end |