lib/bencode.rb in bencode-0.0.1 vs lib/bencode.rb in bencode-0.1.0
- old
+ new
@@ -1,8 +1,36 @@
# Thanks to Daniel Martin and all the other lads at ruby-lang for
# helping me out.
+# 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"
+#
+# == Authors
+#
+# * Daniel Schierbeck
+#
+# == Contributors
+#
+# * Daniel Martin
+# * Phrogz
+#
+# == 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
+# Foundation; either version 2 of the License, or (at your option) any later
+# version.
+#
+# Bencode is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
class Object
# Raises an exception. Subclasses of Object must themselves
# define meaningful #bencode methods.
def bencode
@@ -27,18 +55,20 @@
# as +x:y+, where +y+ is the string and x is the length of the
# string.
#
# "foo".bencode #=> "3:foo"
# "".bencode #=> "0:"
+ #
def bencode
"#{length}:#{self}"
end
# Bdecodes the String object and returns the data serialized
# through bencoding.
#
# "li1ei2ei3ee".bdecode #=> [1, 2, 3]
+ #
def bdecode
Bencode.load(self)
end
# Tests whether the String object is a valid bencoded string.
@@ -54,9 +84,10 @@
class Array
# Bencodes the Array object. Bencoded arrays are represented as
# +lxe+, where x is zero or more bencoded objects.
#
# [1, "foo"].bencode #=> "li1e3:fooe"
+ #
def bencode
"l#{map{|obj| obj.bencode}.join('') }e"
end
end