deep_stringify_keys

deep_stringify_keys() Instance Public methods Returns a new hash with all keys converted to strings. This includes the keys from the root hash and from all nested hashes and arrays. hash = { person: { name: 'Rob', age: '28' } } hash.deep_stringify_keys # => {"person"=>{"name"=>"Rob", "age"=>"28"}}

deep_merge!

deep_merge!(other_hash, &block) Instance Public methods Same as deep_merge, but modifies self.

deep_merge

deep_merge(other_hash, &block) Instance Public methods Returns a new hash with self and other_hash merged recursively. h1 = { a: true, b: { c: [1, 2, 3] } } h2 = { a: false, b: { x: [3, 4, 5] } } h1.deep_merge(h2) #=> { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } } Like with Hash#merge in the standard library, a block can be provided to merge values: h1 = { a: 100, b: 200, c: { c1: 100 } } h2 = { b: 250, c: { c1: 200 } } h1.deep_merge(h2) { |key, this_val, other_val| this_

deep_dup

deep_dup() Instance Public methods Returns a deep copy of hash. hash = { a: { b: 'b' } } dup = hash.deep_dup dup[:a][:c] = 'c' hash[:a][:c] # => nil dup[:a][:c] # => "c"

compact!

compact!() Instance Public methods Replaces current hash with non nil values. hash = { a: true, b: false, c: nil} hash.compact! # => { a: true, b: false} hash # => { a: true, b: false}

compact

compact() Instance Public methods Returns a hash with non nil values. hash = { a: true, b: false, c: nil} hash.compact # => { a: true, b: false} hash # => { a: true, b: false, c: nil} { c: nil }.compact # => {}

assert_valid_keys

assert_valid_keys(*valid_keys) Instance Public methods Validate all keys in a hash match *valid_keys, raising ArgumentError on a mismatch. Note that keys are NOT treated indifferently, meaning if you use strings for keys but assert symbols as keys, this will fail. { name: 'Rob', years: '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: :years. Valid keys are: :name, :age" { name: 'Rob', age: '28' }.assert_valid_keys('name', 'age') # => raises "Argu

from_xml

from_xml(xml, disallowed_types = nil) Class Public methods Returns a Hash containing a collection of pairs when the key is the node name and the value is its content xml = <<-XML <?xml version="1.0" encoding="UTF-8"?> <hash> <foo type="integer">1</foo> <bar type="integer">2</bar> </hash> XML hash = Hash.from_xml(xml) # => {"hash"=>{"foo"=>1, "bar"=>2}} DisallowedType is raised if the XML contains at

from_trusted_xml

from_trusted_xml(xml) Class Public methods Builds a Hash from XML just like Hash.from_xml, but also allows Symbol and YAML.

atomic_write

atomic_write(file_name, temp_dir = Dir.tmpdir) Class Public methods Write to a file atomically. Useful for situations where you don't want other processes or threads to see half-written files. File.atomic_write('important.file') do |file| file.write('hello') end If your temp directory is not on the same filesystem as the file you're trying to write, you can provide a different temporary directory. File.atomic_write('/data/something.important', '/data/tmp') do |file| file.write