Module: BEncode::Dictionary::Hash::InstanceMethods

Defined in:
lib/bencode/dictionary.rb

Instance Method Summary collapse

Instance Method Details

#bencodeString

Encodes an array into a bencoded dictionary. Bencoded dictionaries are encoded as a ‘d’ followed by a list of alternating keys and their corresponding values followed by an ‘e’. Keys appear in sorted order (sorted as raw strings, not alphanumerics).

{:cow => "moo", :seven => 7}.bencode #=> "d3:cow3:moo5:seveni7ee"

Returns:

  • (String)

    the bencoded dictionary



47
48
49
50
51
# File 'lib/bencode/dictionary.rb', line 47

def bencode
  keys.sort{|a, b| a.to_s <=> b.to_s}.collect do |key|
    key.to_s.bencode + self[key].bencode
  end.unshift(:d).push(:e).join
end