Parent

OpenStruct

Public Class Methods

new(hash=nil, &block) click to toggle source

Allows the initialization of an OpenStruct with a setter block:

  person = OpenStruct.new do |o|
    o.name    = 'John Smith'
    o.gender  = :M
    o.age     = 71
  end

You can still provide a hash for initialization purposes, and even combine the two approaches if you wish.

  person = OpenStruct.new(:name => 'John Smith', :age => 31) do |p|
    p.gender = :M
  end

Alternatively you can provide a default block:

  stuff = OpenStruct.new{ |o,k| o[k] = [] }
  stuff.place << :a
  stuff.place << :b
  stuff.place #=> [:a, :b]

A setter block versus a defualt block is determined by the arity of the block. You can not provide both at the same time.

CREDIT: Noah Gibbs, Gavin Sinclair

    # File lib/more/facets/ostruct.rb, line 58
58:   def initialize(hash=nil, &block)
59:     if block && block.arity==2
60:       @table = Hash.new(&block)
61:     else
62:       @table = {}
63:     end
64:     if hash
65:       for k,v in hash
66:         @table[k.to_sym] = v
67:         new_ostruct_member(k)
68:       end
69:     end
70:     if block && block.arity==1
71:       yield self
72:     end
73:   end

Public Instance Methods

[](key) click to toggle source

Access a value in the OpenStruct by key, like a Hash. This increases OpenStruct’s “duckiness”.

  o = OpenStruct.new
  o.t = 4
  o['t']  #=> 4
    # File lib/more/facets/ostruct.rb, line 92
92:   def [](key)
93:     key = key.to_sym unless key.is_a?(Symbol)
94:     @table[key]
95:   end
[]=(key,val) click to toggle source

Set a value in the OpenStruct by key, like a Hash.

  o = OpenStruct.new
  o['t'] = 4
  o.t  #=> 4
     # File lib/more/facets/ostruct.rb, line 103
103:   def []=(key,val)
104:     raise TypeError, "can't modify frozen #{self.class}", caller(1) if self.frozen?
105:     key = key.to_sym unless key.is_a?(Symbol)
106:     @table[key]=val
107:   end
__merge__(other) click to toggle source

Merge hash data creating a new OpenStruct object.

  o = OpenStruct.new
  o.ostruct_merge { :a => 2 }
  o.a  #=> 2
     # File lib/more/facets/ostruct.rb, line 185
185:   def __merge__(other)
186:     o = dup
187:     o.__update__(other)
188:     o
189:   end
__update__(other) click to toggle source

Insert/update hash data on the fly.

  o = OpenStruct.new
  o.ostruct_update { :a => 2 }
  o.a  #=> 2
     # File lib/more/facets/ostruct.rb, line 170
170:   def __update__(other)
171:     raise TypeError, "can't modify frozen #{self.class}", caller(1) if self.frozen?
172:     #other = other.to_hash #to_h?
173:     for k,v in other
174:       @table[k.to_sym] = v
175:     end
176:     self
177:   end
each(&blk) click to toggle source
    # File lib/more/facets/ostruct.rb, line 76
76:   def each(&blk)
77:     @table.each(&blk)
78:   end
instance_delegate() click to toggle source

Provides access to an OpenStruct’s inner table.

  o = OpenStruct.new
  o.a = 1
  o.b = 2
  o.instance_delegate.each { |k, v| puts "#{k} #{v}" }

produces

  a 1
  b 2
     # File lib/more/facets/ostruct.rb, line 125
125:   def instance_delegate
126:     @table
127:   end
Also aliased as: ostruct_delegate
ostruct_delegate() click to toggle source
Alias for: instance_delegate
ostruct_merge(other) click to toggle source

Merge hash data creating a new OpenStruct object.

  o = OpenStruct.new
  o.ostruct_merge { :a => 2 }
  o.a  #=> 2
     # File lib/more/facets/ostruct.rb, line 152
152:   def ostruct_merge(other)
153:     o = dup
154:     o.ostruct_update(other)
155:     o
156:   end
ostruct_update(other) click to toggle source

Insert/update hash data on the fly.

  o = OpenStruct.new
  o.ostruct_update { :a => 2 }
  o.a  #=> 2
     # File lib/more/facets/ostruct.rb, line 137
137:   def ostruct_update(other)
138:     raise TypeError, "can't modify frozen #{self.class}", caller(1) if self.frozen?
139:     #other = other.to_hash  #to_h ?
140:     for k,v in other
141:       @table[k.to_sym] = v
142:     end
143:     self
144:   end
to_h() click to toggle source
    # File lib/more/facets/ostruct.rb, line 81
81:   def to_h
82:     @table.dup
83:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.