InstanceVariableHash

<img src=“https://secure.travis-ci.org/metakeule/instance_variable_hash.png” />

What is it for?

You might have attributes (in a plugin for example) that you want to use as Hash. But if you want to set these attribute from the outside it will be difficult to set parts of this hash at different places.

So it would be better to have all attributes as simple values and have a naming convention that allows conversion into hashes if needed.

Instead of

@config = { :a => {:b => 'val_b', :c => ['c1', 'c2']} }

you would do

@a_b = 'val_b'
@a_c = ['c1', 'c2']

and with this lib you would have a shortcut that allows you to pass a hash:

ivars_from_hash({ :a => {:b => 'val_b', :c => ['c1', 'c2']} })

to get them back as a hash

ivars_to_hash('a') # => {:b => 'val_b', :c => ['c1', 'c2']} }

To use the lib:

require 'rubygems'
require 'instance_variable_hash'

# you might want to use it only for certain classes / objects
class Object
  include InstanceVariableHash
end

That’s all. A very simple library, tested with ruby 1.8 and 1.9.

Contributing

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2011 Marc Rene Arns. See LICENSE.txt for further details.