instance method
merge
Ruby on Rails 2.2.3
Since v2.2.3 Last seen in v2.2.3 PrivateSignature
merge(hash, key, value)
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.
Parameters
-
hashreq -
keyreq -
valuereq
Source
# File activesupport/lib/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
Defined in activesupport/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb line 644
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in XmlSimple