module OodCore::Refinements::HashExtensions
This module provides refinements for manipulating the Ruby {Hash} class.
Public Instance Methods
compact()
click to toggle source
Return a hash with non `nil` values @example
{ a: 1, b: nil, c: 3, d: nil }.compact # => {:a=>1, :c=>3}
@see apidock.com/rails/Hash/compact
# File lib/ood_core/refinements/hash_extensions.rb, line 28 def compact self.select { |_, value| !value.nil? } end
slice(*keys)
click to toggle source
Slices a hash to include only the given keys. Returns a hash containing the given keys. @example
{ a: 1, b: 2, c: 3, d: 4 }.slice(:a, :b) # => {:a=>1, :b=>2}
@see apidock.com/rails/Hash/slice
# File lib/ood_core/refinements/hash_extensions.rb, line 18 def slice(*keys) keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true) keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if has_key?(k) } end
symbolize_keys()
click to toggle source
Symbolize the keys in a {Hash}
# File lib/ood_core/refinements/hash_extensions.rb, line 8 def symbolize_keys self.each_with_object({}) { |(k, v), h| h[k.to_sym] = v } end