Class: Hash

Inherits:
Object show all
Defined in:
lib/backports/1.9/hash.rb,
lib/backports/1.8.7/hash.rb,
lib/backports/rails/hash.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.original_constructorObject



5
# File 'lib/backports/1.8.7/hash.rb', line 5

alias_method :original_constructor, :[]

Instance Method Details

#[](*args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/backports/1.8.7/hash.rb', line 6

def [](*args)
  return original_constructor(*args) unless args.length == 1 && args.first.is_a?(Array)
  {}.tap do |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
  end
end

#default_proc=(proc) ⇒ Object

Standard in Ruby 1.9. See official documentation



12
13
14
# File 'lib/backports/1.9/hash.rb', line 12

def default_proc=(proc)
  replace(Hash.new(&Backports.coerce_to(proc, Proc, :to_proc)).merge!(self))
end

#reverse_merge(other_hash) ⇒ Object

Standard in rails. See official documentation



3
4
5
# File 'lib/backports/rails/hash.rb', line 3

def reverse_merge(other_hash)
  other_hash.merge(self)
end

#reverse_merge!(other_hash) ⇒ Object

Standard in rails. See official documentation



8
9
10
# File 'lib/backports/rails/hash.rb', line 8

def reverse_merge!(other_hash)
  replace(reverse_merge(other_hash))
end

#stringify_keysObject

Standard in rails. See official documentation



23
24
25
# File 'lib/backports/rails/hash.rb', line 23

def stringify_keys
  Hash[map{|key,value| [key.to_s, value] }]
end

#stringify_keys!Object

Standard in rails. See official documentation



28
29
30
# File 'lib/backports/rails/hash.rb', line 28

def stringify_keys!
  self.replace(self.stringify_keys)
end

#symbolize_keysObject

Standard in rails. See official documentation



13
14
15
# 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!Object

Standard in rails. See official documentation



18
19
20
# File 'lib/backports/rails/hash.rb', line 18

def symbolize_keys!
  self.replace(self.symbolize_keys)
end

#try_convert(x) ⇒ Object



5
6
7
8
# File 'lib/backports/1.9/hash.rb', line 5

def try_convert(x)
  return nil unless x.respond_to? :to_hash
  x.to_hash
end