Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#bencodeObject

Bencodes the Hash object. Bencoded hashes are represented as dxe, where x is zero or a power of two bencoded objects. each key is immediately followed by its associated value. All keys must be strings. The keys of the bencoded hash will be in lexicographical order.



101
102
103
104
105
# File 'lib/bencode.rb', line 101

def bencode
  pairs = map{|key, val| [key.to_str.bencode, val.bencode] }
  pairs.sort!{|a, b| a.first <=> b.first }
  "d#{pairs.join('')}e"
end