# OpenStruct (Class)

In: | ostruct.rb
Parent: | Object

ostruct.rb - Python Style Object

     just assign to create field

s = OpenStruct.new s.foo = 25 p s.foo s.bar = 2 p s.bar p s

## Methods

== delete_field inspect method_missing new

## Attributes

table | [R] |

## Public Class methods

new(hash=nil)

[Source]

    # File ostruct.rb, line 12
      def initialize(hash=nil)
        @table = {}
        if hash
          for k,v in hash
            @table[k.to_sym] = v
          end
        end
      end

## Public Instance methods

method_missing(mid, *args)

[Source]

    # File ostruct.rb, line 21
      def method_missing(mid, *args)
        mname = mid.id2name
        len = args.length
        if mname =~ /=$/
          if len != 1
            raise ArgumentError, "wrong # of arguments (#{len} for 1)", caller(1)
          end
          if self.frozen?
            raise TypeError, "can't modify frozen #{self.class}", caller(1)
          end
          mname.chop!
          @table[mname.intern] = args[0]
        elsif len == 0
          @table[mid]
        else
          raise NoMethodError, "undefined method `#{mname}' for #{self}", caller(1)
        end
      end

delete_field(name)

[Source]

    # File ostruct.rb, line 40
      def delete_field(name)
        @table.delete name.to_sym
      end

inspect()

[Source]

    # File ostruct.rb, line 44
      def inspect
        str = "<#{self.class}"
        for k,v in @table
          str << " #{k}=#{v.inspect}"
        end
        str << ">"
      end

==(other)

[Source]

    # File ostruct.rb, line 55
      def ==(other)
        return false unless(other.kind_of?(OpenStruct))
        return @table == other.table
      end

[Validate]