Class: String

Inherits:
Object show all
Defined in:
lib/bencode.rb

Instance Method Summary collapse

Instance Method Details

#bdecodeObject

Bdecodes the String object and returns the data serialized through bencoding.

"li1ei2ei3ee".bdecode   #=> [1, 2, 3]


110
111
112
# File 'lib/bencode.rb', line 110

def bdecode
  Bencode.load(self)
end

#bencodeObject

Bencodes the String object. Bencoded strings are represented as x:y, where y is the string and x is the length of the string.

"foo".bencode   #=> "3:foo"
"".bencode      #=> "0:"


100
101
102
# File 'lib/bencode.rb', line 100

def bencode
  "#{length}:#{self}"
end

#bencoded?Boolean

Tests whether the String object is a valid bencoded string.

Returns:

  • (Boolean)


116
117
118
119
120
121
122
# File 'lib/bencode.rb', line 116

def bencoded?
  bdecode
rescue BdecodeError
  false
else
  true
end