Methods
Public Instance methods
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
[ show source ]
# File lib/facets/core/ostruct/op_fetch.rb, line 13 def [](key) key = key.to_sym unless key.is_a?(Symbol) @table[key] end
Set a value in the OpenStruct by key, like a Hash.
o = OpenStruct.new o['t'] = 4 o.t #=> 4
[ show source ]
# File lib/facets/core/ostruct/op_store.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
Merge hash data creating a new OpenStruct object.
o = OpenStruct.new o.ostruct_merge { :a => 2 } o.a #=> 2
[ show source ]
# File lib/facets/core/ostruct/__merge__.rb, line 13 def __merge__( other ) o = dup o.__update__( other ) o end
Provides public access to the inner table.
[ show source ]
# File lib/facets/core/ostruct/__table__.rb, line 4 def __table__ @table end
Insert/update hash data on the fly.
o = OpenStruct.new o.ostruct_update { :a => 2 } o.a #=> 2
[ show source ]
# File lib/facets/core/ostruct/__update__.rb, line 14 def __update__( other ) hash = other.to_h for k,v in hash @table[k.to_sym] = v end self end
Intercepts some methods to apply to the inner table rather then be defined as part of the open members. As with Kernel#instance all other methods are passed on to the Kernel binding.
o = OpenStruct.new o.a = 1 o.b = 2 o.instance.each { |k, v| puts "#{k} #{v}" }
produces
a 1 b 2
Methods supported:
store, fetch, merge, update, each, key?
[ show source ]
# File lib/facets/core/ostruct/instance.rb, line 25 def instance Functor.new { |op, *args| case op when :[], :[]=, :store, :fetch, :merge, :update, :each, :key? @table.send(op,*args) else super(op,*args) # send(op,*args) end } end
[ show source ]
# File lib/facets/core/ostruct/to_h.rb, line 4 def to_h @table end