Sha256: bb11e2b5afac222ac8fe841e8c1a9c1d01ec1b3634c1b8d24884ed4af964570c

Contents?: true

Size: 525 Bytes

Versions: 3

Compression:

Stored size: 525 Bytes

Contents

class Hash
  def subset *keys, &block
    keys = keys.first if keys.first.is_a? Array
    h = {}
    if keys
      self.each do |k, v|
        h[k] = v if keys.include? k
      end
    else
      self.each do |k, v|
        h[k] = v if block.call k
      end
    end
    h
  end

  def validate_options! *valid_options
    unknown_options = keys - valid_options
    raise "unknown options :#{unknown_options.join(': ')}!" unless unknown_options.empty?
  end

  alias_method :blank?, :empty?
  alias_method :to_h, :to_hash
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ruby_ext-0.5.3 lib/ruby_ext/core/hash.rb
ruby_ext-0.5.2 lib/ruby_ext/core/hash.rb
ruby_ext-0.5.1 lib/ruby_ext/core/hash.rb