Methods
[] []= __merge__ __update__ each instance_delegate new ostruct_delegate ostruct_merge ostruct_update to_h
Public Class methods
new(hash=nil) {|self| ...}

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/lore/facets/ostruct.rb, line 58
  def initialize(hash=nil, &block)
    if block && block.arity==2
      @table = Hash.new(&block)
    else
      @table = {}
    end
    if hash
      for k,v in hash
        @table[k.to_sym] = v
        new_ostruct_member(k)
      end
    end
    if block && block.arity==1
      yield self
    end
  end
Public Instance methods
[](key)

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/lore/facets/ostruct.rb, line 92
  def [](key)
    key = key.to_sym unless key.is_a?(Symbol)
    @table[key]
  end
[]=(key,val)

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

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

Merge hash data creating a new OpenStruct object.

  o = OpenStruct.new
  o.ostruct_merge { :a => 2 }
  o.a  #=> 2
# File lib/lore/facets/ostruct.rb, line 182
  def __merge__(other)
    o = dup
    o.__update__(other)
    o
  end
__update__(other)

Insert/update hash data on the fly.

  o = OpenStruct.new
  o.ostruct_update { :a => 2 }
  o.a  #=> 2
# File lib/lore/facets/ostruct.rb, line 168
  def __update__(other)
    #other = other.to_hash #to_h?
    for k,v in other
      @table[k.to_sym] = v
    end
    self
  end
each(&blk)
# File lib/lore/facets/ostruct.rb, line 76
  def each(&blk)
    @table.each(&blk)
  end
instance_delegate()

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
This method is also aliased as ostruct_delegate
# File lib/lore/facets/ostruct.rb, line 125
  def instance_delegate
    @table
  end
ostruct_delegate()

Alias for instance_delegate

ostruct_merge(other)

Merge hash data creating a new OpenStruct object.

  o = OpenStruct.new
  o.ostruct_merge { :a => 2 }
  o.a  #=> 2
# File lib/lore/facets/ostruct.rb, line 150
  def ostruct_merge(other)
    o = dup
    o.ostruct_update(other)
    o
  end
ostruct_update(other)

Insert/update hash data on the fly.

  o = OpenStruct.new
  o.ostruct_update { :a => 2 }
  o.a  #=> 2
# File lib/lore/facets/ostruct.rb, line 136
  def ostruct_update(other)
    #other = other.to_hash  #to_h ?
    for k,v in other
      @table[k.to_sym] = v
    end
    self
  end
to_h()
# File lib/lore/facets/ostruct.rb, line 81
  def to_h
    @table.dup
  end