Adds a new key/value pair to an existing Hash. If the key to be added does already exist and the existing value associated with key is not an Array, it will be converted into an Array. Then the new value is appended to that Array.
| hash: | Hash to add key/value pair to. |
| key: | Key to be added. |
| value: | Value to be associated with key. |
Source Code
# File active_support/vendor/xml-simple-1.0.11/xmlsimple.rb, line 644 def merge(hash, key, value) if value.instance_of?(String) value = normalise_space(value) if @options['normalisespace'] == 2 # do variable substitutions unless @_var_values.nil? || @_var_values.empty? value.gsub!(/\$\{(\w+)\}/) { |x| get_var($1) } end # look for variable definitions if @options.has_key?('varattr') varattr = @options['varattr'] if hash.has_key?(varattr) set_var(hash[varattr], value) end end end #patch for converting keys to symbols if @options.has_key?('keytosymbol') if @options['keytosymbol'] == true key = key.to_s.downcase.to_sym end end if hash.has_key?(key) if hash[key].instance_of?(Array) hash[key] << value else hash[key] = [ hash[key], value ] end elsif value.instance_of?(Array) # Handle anonymous arrays. hash[key] = [ value ] else if force_array?(key) hash[key] = [ value ] else hash[key] = value end end hash end
<code/>and<pre/>for code samples.