lib/ronin/binary/struct.rb in ronin-support-0.5.0 vs lib/ronin/binary/struct.rb in ronin-support-0.5.1

- old
+ new

@@ -37,11 +37,11 @@ # # end # # pkt = Packet.new # pkt.length = 5 - # pkt.data = 'hello' + # pkt.data = 'hello' # # buffer = pkt.pack # # => "\x00\x00\x00\x05hello\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" # # new_pkt = Packet.unpack(buffer) @@ -94,11 +94,11 @@ # # @param [Hash] options # Unpacking options. # # @option options [:little, :big, :network] :endian - # The endianness to apply to types. + # The endianness to apply to the types. # # @return [Struct] # The newly unpacked structure. # def self.unpack(data,options={}) @@ -129,14 +129,12 @@ # # @raise [ArgumentError] # The structure does not contain the field. # def [](name) - if field?(name) - send(name) - else - raise(ArgumentError,"no such field '#{name}'") + if field?(name) then send(name) + else raise(ArgumentError,"no such field '#{name}'") end end # # Writes a value to the structure. @@ -152,14 +150,12 @@ # # @raise [ArgumentError] # The structure does not contain the field. # def []=(name,value) - if field?(name) - send("#{name}=",value) - else - raise(ArgumentError,"no such field '#{name}'") + if field?(name) then send("#{name}=",value) + else raise(ArgumentError,"no such field '#{name}'") end end # # The values within the structure. @@ -168,23 +164,19 @@ # The values of the fields. # def values normalize = lambda { |value| case value - when Struct - value.values - else - value + when Struct then value.values + else value end } self.class.layout.map do |name| case (value = self[name]) - when Array - value.map(&normalize) - else - normalize[value] + when Array then value.map(&normalize) + else normalize[value] end end end # @@ -206,11 +198,11 @@ # # @param [Hash] options # Pack options. # # @option options [:little, :big, :network] :endian - # The endianness to apply to types. + # The endianness to apply to the types. # # @return [String] # The packed structure. # def pack(options={}) @@ -225,11 +217,11 @@ # # @param [Hash] options # Unpack options. # # @option options [:little, :big, :network] :endian - # The endianness to apply to types. + # The endianness to apply to the types. # # @return [Struct] # The unpacked structure. # def unpack(data,options={}) @@ -381,14 +373,12 @@ # # @return [:little, :big, :network, nil] # The endianness of the structure. # def self.endian(type=nil) - if type - @endian = type.to_sym - else - @endian + if type then @endian = type.to_sym + else @endian end end # # The layout of the structure. @@ -482,17 +472,13 @@ # create an array of values Array.new(length) { |index| default(type) } end else if type.kind_of?(Symbol) - if Template::INT_TYPES.include?(type) - 0 - elsif Template::FLOAT_TYPES.include?(type) - 0.0 - elsif Template::CHAR_TYPES.include?(type) - "\0" - elsif Template::STRING_TYPES.include?(type) - '' + if Template::INT_TYPES.include?(type) then 0 + elsif Template::FLOAT_TYPES.include?(type) then 0.0 + elsif Template::CHAR_TYPES.include?(type) then "\0" + elsif Template::STRING_TYPES.include?(type) then '' end elsif type < Struct type.new end end