Skip to content

Commit

Permalink
Merge pull request #10 from rawsyntax/master
Browse files Browse the repository at this point in the history
Keep core extensions in separate file(s)
  • Loading branch information
dasch committed Feb 11, 2014
2 parents b1ef475 + 3a4ef86 commit 6d28189
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
14 changes: 12 additions & 2 deletions lib/bencode/core_ext/string.rb
Expand Up @@ -2,13 +2,23 @@
class String
#
# Bencodes the String object. Bencoded strings are represented
# as <code>x</code>:<code>y</code>, where +y+ is the string and +x+
# as <code>x</code>:<code>y</code>, 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
end
14 changes: 1 addition & 13 deletions lib/bencode/decode.rb
Expand Up @@ -22,7 +22,7 @@ def self.load(str, opts = {})
scanner = BEncode::Parser.new(str)
obj = scanner.parse!
raise BEncode::DecodeError unless (opts[:ignore_trailing_junk] || scanner.eos?)
return obj
obj
end

# Decodes the file located at +path+.
Expand All @@ -34,15 +34,3 @@ def self.load_file(path, opts = {})
load(File.open(path, 'rb').read, opts)
end
end

class String
#
# Bdecodes the String object and returns the data serialized
# through bencoding.
#
# "li1ei2ei3ee".bdecode #=> [1, 2, 3]
#
def bdecode
BEncode.load(self)
end
end
15 changes: 8 additions & 7 deletions lib/bencode/parser.rb
Expand Up @@ -5,13 +5,14 @@ class Parser
attr_reader :stream

def initialize(stream)
if stream.kind_of?(IO) || stream.kind_of?(StringIO)
@stream = stream
elsif stream.respond_to? :string
@stream = StringIO.new stream.string
elsif stream.respond_to? :to_s
@stream = StringIO.new stream.to_s
end
@stream =
if stream.kind_of?(IO) || stream.kind_of?(StringIO)
stream
elsif stream.respond_to? :string
StringIO.new stream.string
elsif stream.respond_to? :to_s
StringIO.new stream.to_s
end
end

def parse!
Expand Down

0 comments on commit 6d28189

Please sign in to comment.