Class | BasicStruct |
In: |
lib/hashery/basicstruct.rb
|
Parent: | BasicObject |
BasicObject 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 BasicObject, almost any slot can be defined. BasicObject is a subclass of BasicObject to ensure all method slots, except those that are absolutely essential, are open for use.
Unlike a Hash, all BasicObject‘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 BasicObject 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).
BasicObject.new(:a=>1).default!(0)