Sha256: d5d0bdd88324241d7068c8422a9a1f59a1367125526370a54d889176240ff0b6

Contents?: true

Size: 641 Bytes

Versions: 5

Compression:

Stored size: 641 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

  # def inspect
  #   "{" + collect{|k, v| "#{k}: #{v}"}.join(', ') + "}"
  # end
  # alias_method :to_s, :inspect

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ruby_ext-0.5.8 lib/ruby_ext/core/hash.rb
ruby_ext-0.5.7 lib/ruby_ext/core/hash.rb
ruby_ext-0.5.6 lib/ruby_ext/core/hash.rb
ruby_ext-0.5.5 lib/ruby_ext/core/hash.rb
ruby_ext-0.5.4 lib/ruby_ext/core/hash.rb