Skip to content

Commit 6d28189

Browse files
committed
Merge pull request #10 from rawsyntax/master
Keep core extensions in separate file(s)
2 parents b1ef475 + 3a4ef86 commit 6d28189

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

lib/bencode/core_ext/string.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@
22
class 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
1424
end

lib/bencode/decode.rb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff 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
3636
end
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

lib/bencode/parser.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff 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!

0 commit comments

Comments
 (0)