Sha256: 286ff38cd51fd3774ae380f2a33e77c86b04d4f7cbc26b4f3faee72f4b1e7cc6
Contents?: true
Size: 1.46 KB
Versions: 3
Compression:
Stored size: 1.46 KB
Contents
# encoding: UTF-8 require 'uri' module BEncodr module String module Generic module InstanceMethods # Encodes object into a bencoded string. BEncoded strings are length-prefixed base ten followed by a colon and # the string. # # :symbol.bencodr #=> "6:symbol" # # @return [::String] the bencoded string def bencode (respond_to?(:to_s) ? to_s : to_str).bencode end end end # Registers a class as an object that can be converted into a bencoded string. Class must have instance method to_s # or to_str. # # class MyClass # def to_s # "string" # end # end # # BEncode::String.register MyClass # my_class = MyClass.new # my_class.bencodr #=> "6:string" # # @param [Class#to_s, Class#to_str] type the class to add the bencodr instance method to def self.register(type) type.send :include, Generic::InstanceMethods end register Symbol register URI::Generic module String module InstanceMethods # Encodes a string into a bencoded string. BEncoded strings are length-prefixed base ten followed by a colon and # the string. # # "string".bencodr #=> "6:string" # # @return [::String] the bencoded string def bencode [length, ':', self].join end end ::String.send :include, InstanceMethods end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
bencodr-1.2.0 | lib/bencodr/string.rb |
bencodr-1.1.0 | lib/bencodr/string.rb |
bencodr-1.0.1 | lib/bencodr/string.rb |