File tree Expand file tree Collapse file tree 3 files changed +21
-22
lines changed
Expand file tree Collapse file tree 3 files changed +21
-22
lines changed Original file line number Diff line number Diff line change 22class String
33 #
44 # Bencodes the String object. Bencoded strings are represented
5- # as <code>x</code>:<code>y</code>, where +y+ is the string and +x+
5+ # as <code>x</code>:<code>y</code>, where +y+ is the string and +x+
66 # is the length of the string.
7- #
7+ #
88 # "foo".bencode #=> "3:foo"
99 # "".bencode #=> "0:"
1010 #
1111 def bencode
1212 "#{ length } :#{ self } "
1313 end
14+
15+ #
16+ # Bdecodes the String object and returns the data serialized
17+ # through bencoding.
18+ #
19+ # "li1ei2ei3ee".bdecode #=> [1, 2, 3]
20+ #
21+ def bdecode
22+ BEncode . load ( self )
23+ end
1424end
Original file line number Diff line number Diff line change @@ -22,7 +22,7 @@ def self.load(str, opts = {})
2222 scanner = BEncode ::Parser . new ( str )
2323 obj = scanner . parse!
2424 raise BEncode ::DecodeError unless ( opts [ :ignore_trailing_junk ] || scanner . eos? )
25- return obj
25+ obj
2626 end
2727
2828 # Decodes the file located at +path+.
@@ -34,15 +34,3 @@ def self.load_file(path, opts = {})
3434 load ( File . open ( path , 'rb' ) . read , opts )
3535 end
3636end
37-
38- class String
39- #
40- # Bdecodes the String object and returns the data serialized
41- # through bencoding.
42- #
43- # "li1ei2ei3ee".bdecode #=> [1, 2, 3]
44- #
45- def bdecode
46- BEncode . load ( self )
47- end
48- end
Original file line number Diff line number Diff line change @@ -5,13 +5,14 @@ class Parser
55 attr_reader :stream
66
77 def initialize ( stream )
8- if stream . kind_of? ( IO ) || stream . kind_of? ( StringIO )
9- @stream = stream
10- elsif stream . respond_to? :string
11- @stream = StringIO . new stream . string
12- elsif stream . respond_to? :to_s
13- @stream = StringIO . new stream . to_s
14- end
8+ @stream =
9+ if stream . kind_of? ( IO ) || stream . kind_of? ( StringIO )
10+ stream
11+ elsif stream . respond_to? :string
12+ StringIO . new stream . string
13+ elsif stream . respond_to? :to_s
14+ StringIO . new stream . to_s
15+ end
1516 end
1617
1718 def parse!
You can’t perform that action at this time.
0 commit comments