lib/bencode.rb in bencode-0.3.0 vs lib/bencode.rb in bencode-0.3.1

- old
+ new

@@ -3,44 +3,40 @@ # Bencode is a Ruby implementation of the Bencode data serialization # format used in the BitTorrent protocol. # # == Synopsis # -# "foobar".bencode #=> "6:foobar" -# 42.bencode #=> "i42e" -# [1, 2, 3].bencode #=> "li1ei2ei3ee" -# # Bencoding (pronounced _bee-encode_) is a simple protocol, consiting of # only 4 value types. # -# === Integers === +# === Integers # # An integer is encoded an _i_ followed by the numeral itself, followed # by an _e_. Leading zeros are not allowed. Negative values are prefixed # with a minus sign. # # 42.bencode #=> "i42e" # -2.bencode #=> "i-2e" # 0.bencode #=> "i0e" # -# === Strings === +# === Strings # # Strings are a sequence of zero or more bytes. It is encoded as # _<length>:<contents>_, where _length_ is the lenth of _contents_. _length_ # must be non-negative. # # "".bencode #=> "0:" # "foo".bencode #=> "3:foo" # -# === Lists === +# === Lists # # Lists are encoded as _l_ followed by the elements, followed by _e_. # There is no element seperator. # # [1, 2, 3].bencode #=> "li1ei2ei3ee" # -# === Dictionaries === +# === Dictionaries # # Dictionaries are encoded as _d<contents>e_, where _contents_ is a sequence # of keys and values. Each value is immediately preceded by a key. Keys must # be strings, and will appear in lexicographical order. # @@ -54,10 +50,11 @@ # # == Contributors # # * Daniel Martin # * Phrogz +# * Julien Pervillé # # == Copyright # # Bencode is free software; you can redistribute it and/or modify it under the # terms of the GNU General Public License as published by the Free Software @@ -136,11 +133,10 @@ # +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. def bencode - pairs = map{|key, val| [key.to_str.bencode, val.bencode] } - pairs.sort!{|a, b| a.first <=> b.first } + pairs = sort.map{|key, val| [key.to_str.bencode, val.bencode]} "d#{pairs.join('')}e" end end class IO