Sha256: f340fe0706228ef44ea6a4016e1a1ae5b64d4130a5e45e0df70ba354a772580a
Contents?: true
Size: 1.53 KB
Versions: 3
Compression:
Stored size: 1.53 KB
Contents
# encoding: UTF-8 module BEncodr module List module Generic module InstanceMethods # Encodes object into a bencoded list. BEncoded strings are length-prefixed base ten followed by a colon and # the string. Object must implement to_a or to_ary. # # [].bencodr #=> "le" # # @return [::String] the bencoded list def bencode (respond_to?(:to_ary) ? to_ary : to_a).bencode end end end # Registers a class as an object that can be converted into a bencoded list. Class must have instance method to_a # or to_ary. # # class MyClass # def to_a # [1, :cat] # end # end # # BEncodr::Integer.register MyClass # my_class = MyClass.new # my_class.bencodr #=> "li1e3:cate" # # @param [Class#to_a, Class#to_ary] type the class to add the bencodr instance method to def self.register(type) type.send :include, Generic::InstanceMethods end module Array module InstanceMethods # Encodes an array into a bencoded list. Bencoded lists are encoded as an 'l' followed by their elements (also # bencoded) followed by an 'e'. # # [:eggs, "ham", 3, 4.1].bencodr #=> "l4:eggs3:hami3ei4ee" # # @return [::String] the bencoded list def bencode collect do |element| element.bencode end.unshift(:l).push(:e).join end end ::Array.send :include, InstanceMethods end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
bencodr-1.2.0 | lib/bencodr/list.rb |
bencodr-1.1.0 | lib/bencodr/list.rb |
bencodr-1.0.1 | lib/bencodr/list.rb |