File: openobject.rb

# openobject.rb

Path: | lib/more/facets/openobject.rb
Last Update: | Tue Aug 10 17:25:06 -0600 2010

# OpenObject

OpenObject is very similar to Ruby‘s own OpenStruct, but it offers some useful advantages. With OpenStruct slots with the same name‘s as predefined Object methods can not be used. With OpenObject any slot can be defined, OpendObject is also a bit faster becuase it is based on a Hash, not method signitures.

Because OpenObject is a subclass of Hash, it can do just about everything a Hash can do, except that most public methods have been made protected and thus available only internally or via send.

OpenObject will also clobber any method for which a slot is defined. Even generally very important methods can be clobbered, like instance_eval. So be aware of this. OpenObject should be used in well controlled scenarios.

If you wish to pass an OpenObject to a routine that normal takes a Hash, but are uncertain it can handle the distictions properly you can convert easily to a Hash using to_hash! and the result will automatically be converted back to an OpenObject on return.

      o = OpenObject.new(:a=>1,:b=>2)
      o.as_hash!{ |h| h.update(:a=>6) }
      o #=> #<OpenObject {:a=>6,:b=>2}>

Finally, unlike a regular Hash, all OpenObject‘s keys are symbols and all keys are converted to such using to_sym on the fly.

## Authors

- Thomas Sawyer
- George Moschovitis

## Copying

Copyright (c) 2005 Thomas Sawyer, George Moschovitis

Ruby License

This module is free software. You may use, modify, and/or redistribute this software under the same terms as Ruby.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

### Required files

facets/hash/to_h facets/hash/to_proc facets/kernel/object_class facets/kernel/object_hexid

[Validate]