Sha256: a68906739114b361383db31f44b4679b2e6d1bec1d08ebb22004262a6038045c

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

Hash.class_eval do
  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 reverse_merge(other_hash)
    other_hash.merge(self)
  end

  def reverse_merge!(other_hash)
    merge!( other_hash ){|key,left,right| left }
  end

  # Haml relies on :inspect default format and it brokes haml, but I prefer new hash notation,
  # disable it if You use Haml.
  # unless $dont_extend_hash_inspect
  #   def inspect
  #     "{" + collect{|k, v| "#{k}: #{v}"}.join(', ') + "}"
  #   end
  #   alias_method :to_s, :inspect
  # end

  alias_method :blank?, :empty?

  alias_method :to_h, :to_hash

  # OpenObject.

  def to_openobject deep = false
    OpenObject.initialize_from self, deep
  end
  alias_method :to_oo, :to_openobject

  alias_method :eql_without_oo, :==
  def == other
    true if self.equal? other
    other == self if other.is_a? OpenObject
    eql_without_oo other
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby_ext-4.0.0 lib/ruby_ext/core/hash.rb