I want to merge a value into a Thor option hash.
If I just use merge I get an error, HashWithIndifferentAccess
I have read the documentation but I have difficulties to understand how to get it to work. I guess I hope this question will help me to both find an answer on the question how to merge a value into this kind of hash and understand how to read documentation.
p options.inspect
#=> "{\"ruby\"=>\"/Users/work/.rbenv/versions/1.9.2-p290/bin/ruby\"}"
p options.merge!(:a => true)
#=> hash_with_indifferent_access.rb:26:in `[]=': can't modify frozen hash (RuntimeError)
The hash is frozen:
You can copy
optionsto a new hash (will be unfrozen) and modifying that instead.Or if you want it to be even briefer:
The Thor library returns a frozen hash by default, so another option would be to modify the library to return unfrozen hashes, but I think the first solution is good enough.
Below is a link to the source code for Thor’s parse function, you’ll notice it freezes the “assigns” return hash prior to actually returning it (go to the bottom of the page, and under
(Object) parse(args), click ‘View Source’. The freezing is on line 83 of the source snippet.)http://rubydoc.info/github/wycats/thor/master/Thor/Options