# File lib/backports/1.8.7/hash.rb, line 7 def [](*args) return constructor_without_key_value_pair_form(*args) unless args.length == 1 && args.first.is_a?(Array) h = {} args.first.each do |arr| next unless arr.respond_to? :to_ary arr = arr.to_ary next unless (1..2).include? arr.size h[arr.at(0)] = arr.at(1) end h end
# File lib/backports/1.9.1/hash.rb, line 4 def try_convert(x) Backports.try_convert(x, Hash, :to_hash) end
Standard in Ruby 1.9. See official documentation
# File lib/backports/1.9.1/hash.rb, line 15 def assoc(key) val = fetch(key) do return find do |k, v| [k, v] if k == key end end [key, val] end
Standard in Ruby 1.9. See official documentation
# File lib/backports/1.9.1/hash.rb, line 10 def default_proc=(proc) replace(Hash.new(&Backports.coerce_to(proc, Proc, :to_proc)).merge!(self)) end
Ruby 1.8.6 doesn't define a Hash specific eql? method.
# File lib/backports/1.8.7/hash.rb, line 31 def eql?(other) other.is_a?(Hash) && size == other.size && all? do |key, value| other.fetch(key){return false}.eql?(value) end end
Ruby 1.8.6 doesn't define a Hash specific hash method
# File lib/backports/1.8.7/hash.rb, line 22 def hash h = 0 each do |key, value| h ^= key.hash ^ value.hash end h end
# File lib/backports/1.9.2/hash.rb, line 2 def keep_if return to_enum(:keep_if) unless block_given? delete_if{|key, value| ! yield key, value} end
Standard in Ruby 1.9. See official documentation
# File lib/backports/1.9.1/hash.rb, line 26 def rassoc(value) k = key(value) v = fetch(k){return nil} [k, fetch(k)] if k || v == value end
Standard in rails. See official documentation
# File lib/backports/rails/hash.rb, line 3 def reverse_merge(other_hash) other_hash.merge(self) end
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
# File lib/backports/1.9.2/hash.rb, line 7 def select!(&block) return to_enum(:select!) unless block_given? reject!{|key, value| ! yield key, value} end
# File lib/backports/force/hash_select.rb, line 3 def select_with_hash_return return to_enum(:select) unless block_given? Hash[select_without_hash_return{|k, v| yield [k, v]}] end
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
Standard in rails. See official documentation
# File lib/backports/rails/hash.rb, line 28 def stringify_keys! self.replace(self.stringify_keys) end
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
Standard in rails. See official documentation
# File lib/backports/rails/hash.rb, line 18 def symbolize_keys! self.replace(self.symbolize_keys) end