class Hash
Public Class Methods
[](*args)
click to toggle source
# File lib/backports/1.8.7/hash/constructor.rb, line 6 def [](*args) if args.length == 1 arg = args.first if (h = Backports.try_convert(arg, Hash, :to_hash)) return allocate.replace(h) end if (kvps = Backports.is_array?(arg)) h = {} kvps.each do |elem| next unless arr = Backports.is_array?(elem) next unless (1..2).include? arr.size h[arr.at(0)] = arr.at(1) end return h end end constructor_without_key_value_pair_form(*args) end
Also aliased as: constructor_without_key_value_pair_form
try_convert(x)
click to toggle source
# File lib/backports/1.9.1/hash/try_convert.rb, line 4 def Hash.try_convert(x) Backports.try_convert(x, Hash, :to_hash) end
Public Instance Methods
<(hash)
click to toggle source
# File lib/backports/2.3.0/hash/lt.rb, line 4 def <(hash) hash = Backports.coerce_to_hash(hash) return false unless size < hash.size each do |k, v| v2 = hash.fetch(k){ return false } return false unless v2 == v end true end
<=(hash)
click to toggle source
# File lib/backports/2.3.0/hash/lte.rb, line 4 def <=(hash) hash = Backports.coerce_to_hash(hash) return false unless size <= hash.size each do |k, v| v2 = hash.fetch(k){ return false } return false unless v2 == v end true end
>(hash)
click to toggle source
# File lib/backports/2.3.0/hash/gt.rb, line 5 def >(hash) hash = Backports.coerce_to_hash(hash) hash < self end
>=(hash)
click to toggle source
# File lib/backports/2.3.0/hash/gte.rb, line 5 def >=(hash) hash = Backports.coerce_to_hash(hash) hash <= self end
any_with_pattern?(pattern = Backports::Undefined, &block)
click to toggle source
# File lib/backports/2.5.0/enumerable/any.rb, line 35 def any_with_pattern?(pattern = Backports::Undefined, &block) return any_without_pattern?(&block) if Backports::Undefined == pattern each_entry { |x| return true if pattern === x } false end
assoc(key)
click to toggle source
# File lib/backports/1.9.1/hash/assoc.rb, line 3 def assoc(key) val = fetch(key) do return find do |k, v| [k, v] if k == key end end [key, val] end
compact()
click to toggle source
# File lib/backports/2.4.0/hash/compact.rb, line 2 def compact h = {} each do |key, value| h[key] = value unless value == nil end h end
compact!()
click to toggle source
# File lib/backports/2.4.0/hash/compact.rb, line 10 def compact! reject! {|_key, value| value == nil} end
default_proc=(proc)
click to toggle source
# File lib/backports/1.9.1/hash/default_proc.rb, line 5 def default_proc=(proc) if proc == nil # nil accepted in Ruby 2.0 self.default = nil self else replace(Hash.new(&Backports.coerce_to(proc, Proc, :to_proc)).merge!(self)) end end
default_proc_with_nil=(proc)
click to toggle source
# File lib/backports/2.0.0/hash/default_proc.rb, line 6 def default_proc_with_nil=(proc) if proc == nil self.default = nil self else self.default_proc_without_nil=(proc) end end
dig(key, *rest)
click to toggle source
# File lib/backports/2.3.0/hash/dig.rb, line 3 def dig(key, *rest) val = self[key] return val if rest.empty? || val == nil raise TypeError, "#{val.class} does not have #dig method" unless val.respond_to? :dig val.dig(*rest) end
eql?(other)
click to toggle source
# File lib/backports/1.8.7/hash/hash.rb, line 10 def eql?(other) other.is_a?(Hash) && size == other.size && all? do |key, value| value.eql?(other.fetch(key){return false}) end end
fetch_values(*keys, &block)
click to toggle source
# File lib/backports/2.3.0/hash/fetch_values.rb, line 3 def fetch_values(*keys, &block) keys.map do |k| fetch(k, &block) end end
hash()
click to toggle source
# File lib/backports/1.8.7/hash/hash.rb, line 2 def hash h = 0 each do |key, value| h ^= key.hash ^ value.hash end h end
keep_if() { |key, value| ... }
click to toggle source
# File lib/backports/1.9.2/hash/keep_if.rb, line 3 def keep_if return to_enum(:keep_if) unless block_given? delete_if{|key, value| ! yield key, value} end
merge_with_backports(first = {}, *others, &block)
click to toggle source
# File lib/backports/2.6.0/hash/merge.rb, line 5 def merge_with_backports(first = {}, *others, &block) merge_without_backports(first, &block). merge!(*others, &block) end
merge_with_backports!(*hashes, &block)
click to toggle source
# File lib/backports/2.6.0/hash/merge.rb, line 13 def merge_with_backports!(*hashes, &block) hashes.each do |h| merge_without_backports!(h, &block) end self end
rassoc(value)
click to toggle source
# File lib/backports/1.9.1/hash/rassoc.rb, line 3 def rassoc(value) k = key(value) v = fetch(k){return nil} [k, fetch(k)] if k || v == value end
reverse_merge(other_hash)
click to toggle source
Standard in rails. See official documentation
# File lib/backports/rails/hash.rb, line 3 def reverse_merge(other_hash) other_hash.merge(self) end
reverse_merge!(other_hash)
click to toggle source
Standard in rails. See official documentation
# File lib/backports/rails/hash.rb, line 8 def reverse_merge!(other_hash) replace(reverse_merge(other_hash)) end
select!() { |key, value| ... }
click to toggle source
# File lib/backports/1.9.2/hash/select.rb, line 3 def select! return to_enum(:select!) unless block_given? raise "can't modify frozen hash" if frozen? # reject! won't do it for empty hashes... reject!{|key, value| ! yield key, value} end
select_with_hash_return()
click to toggle source
# File lib/backports/force/hash_select.rb, line 4 def select_with_hash_return return to_enum(:select) unless block_given? Hash[select_without_hash_return{|k, v| yield [k, v]}] end
slice(*keys)
click to toggle source
# File lib/backports/2.5.0/hash/slice.rb, line 2 def slice(*keys) h = {} keys.each { |k| h[k] = self[k] if key?(k) } h end
stringify_keys()
click to toggle source
Standard in rails. See official documentation
# File lib/backports/rails/hash.rb, line 23 def stringify_keys Hash[map{|key,value| [key.to_s, value] }] end
stringify_keys!()
click to toggle source
Standard in rails. See official documentation
# File lib/backports/rails/hash.rb, line 28 def stringify_keys! self.replace(self.stringify_keys) end
symbolize_keys()
click to toggle source
Standard in rails. See official documentation
# File lib/backports/rails/hash.rb, line 13 def symbolize_keys Hash[map{|key,value| [(key.to_sym rescue key) || key, value] }] end
symbolize_keys!()
click to toggle source
Standard in rails. See official documentation
# File lib/backports/rails/hash.rb, line 18 def symbolize_keys! self.replace(self.symbolize_keys) end
to_h()
click to toggle source
# File lib/backports/2.0.0/hash/to_h.rb, line 3 def to_h self.class == Hash ? self : {}.replace(self) end
to_h_with_block(&block)
click to toggle source
# File lib/backports/2.6.0/hash/to_h.rb, line 8 def to_h_with_block(&block) return to_h_without_block unless block map(&block).to_h end
to_proc()
click to toggle source
# File lib/backports/2.3.0/hash/to_proc.rb, line 3 def to_proc h = self Proc.new{|*args| h[*args]} end
transform_keys() { |key] = value| ... }
click to toggle source
# File lib/backports/2.5.0/hash/transform_keys.rb, line 2 def transform_keys return to_enum(:transform_keys){ size } unless block_given? h = {} each do |key, value| h[yield key] = value end h end
transform_keys!() { |key| ... }
click to toggle source
# File lib/backports/2.5.0/hash/transform_keys.rb, line 11 def transform_keys! return enum_for(:transform_keys!) { size } unless block_given? merge!({}) if frozen? keys.each do |key| self[yield(key)] = delete(key) end self end
transform_values() { |value| ... }
click to toggle source
# File lib/backports/2.4.0/hash/transform_values.rb, line 2 def transform_values return to_enum(:transform_values){ size } unless block_given? h = {} each do |key, value| h[key] = yield value end h end
transform_values!() { |value| ... }
click to toggle source
# File lib/backports/2.4.0/hash/transform_values.rb, line 11 def transform_values! return to_enum(:transform_values!){ size } unless block_given? reject!{} if frozen? # Force error triggerring if frozen, in case of empty array each do |key, value| self[key] = yield value end end