Module: Packable::ClassMethods
- Defined in:
- lib/packable/mixin.rb
Instance Method Summary collapse
-
#read_packed(io, options) ⇒ Object
Default
read_packed
calls either the instance methodread_packed
or the class methodunpack_string
. - #unpack(s, options = :default) ⇒ Object
Instance Method Details
#read_packed(io, options) ⇒ Object
Default read_packed
calls either the instance method read_packed
or the class method unpack_string
. Choose:
-
define a class method
read_packed
that returns the newly read object -
define an instance method
read_packed
which reads the io intoself
-
define a class method
unpack_string
that reads and returns an object from the string. In this case, options should be specified!
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/packable/mixin.rb', line 46 def read_packed(io, ) if method_defined? :read_packed mandatory = instance_method(:initialize).arity mandatory = -1-mandatory if mandatory < 0 obj = new(*[nil]*mandatory) obj.read_packed(io, ) obj else len = [:bytes] s = len ? io.read_exactly(len) : io.read unpack_string(s, ) unless s.nil? end end |
#unpack(s, options = :default) ⇒ Object
35 36 37 38 |
# File 'lib/packable/mixin.rb', line 35 def unpack(s, = :default) return s.unpack().first if .is_a? String StringIO.new(s).packed.read(self, ) end |