Class | OpenObject |
In: |
lib/hashery/openobject.rb
|
Parent: | BasicObject |
OpenObject is very similar to Ruby‘s own OpenStruct, but it offers some advantages. With OpenStruct, slots with the same name as predefined Object methods cannot be used. With OpenObject, almost any slot can be defined. OpenObject is a subclass of BasicObject to ensure all method slots, except those that are absolutely essential, are open for use.
Unlike a Hash, all OpenObject‘s keys are symbols and all keys are converted to such using to_sym on the fly.
PUBLIC_METHODS = /(^__|^instance_|^object_|^\W|^as$|^send$|^class$|\?$)/ protected(*public_instance_methods.select{ |m| m !~ PUBLIC_METHODS })
Inititalizer for OpenObject is slightly different than that of Hash. It does not take a default parameter, but an initial priming Hash, like OpenStruct. The initializer can still take a default block however. To set the default value use default!(value).
OpenObject.new(:a=>1).default!(0)