Class: OpenStruct

Inherits:
Object show all
Defined in:
lib/nano/ostruct/%5B%5D.rb,
lib/nano/ostruct/%5B%5D%3D.rb,
lib/nano/ostruct/ostruct_update.rb

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object

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


13
14
15
16
# File 'lib/nano/ostruct/%5B%5D.rb', line 13

def [](key)
  key = key.to_sym unless key.is_a?(Symbol)
  @table[key]
end

#[]=(key, val) ⇒ Object

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

o = OpenStruct.new
o['t'] = 4
o.t  #=> 4

Raises:

  • (TypeError)


12
13
14
15
16
# File 'lib/nano/ostruct/%5B%5D%3D.rb', line 12

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

#ostruct_update(hash) ⇒ Object

Insert/update hash data on the fly.

o = OpenStruct.new
o.ostruct_update { :a => 2 }
o.a  #=> 2


12
13
14
15
16
17
18
# File 'lib/nano/ostruct/ostruct_update.rb', line 12

def ostruct_update( hash )
  if hash
    for k,v in hash
      @table[k.to_sym] = v
    end
  end
end