class SliceableHash

Public Class Methods

new(hash) click to toggle source
# File lib/ext/sliceable_hash.rb, line 3
def initialize(hash)
  @hash = hash
end

Public Instance Methods

if_string_not_empty(val) click to toggle source
# File lib/ext/sliceable_hash.rb, line 13
def if_string_not_empty(val)
  val.kind_of?(String) ? !val.empty? : true
end
slice(*keys) click to toggle source

Return a hash that includes only the given keys.

# File lib/ext/sliceable_hash.rb, line 8
def slice(*keys)
  keys.map! { |key| @hash.convert_key(key) } if @hash.respond_to?(:convert_key, true)
  keys.each_with_object(@hash.class.new) { |k, hash| hash[k] = @hash[k] if @hash.has_key?(k) && if_string_not_empty(@hash[k]) }
end