aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/misc.rb
blob: d2c3d4a045008dac70641a302ca725ab282737eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def silence_warnings
  old_verbose, $VERBOSE = $VERBOSE, nil
  begin
    yield
  ensure
    $VERBOSE = old_verbose
  end
end

class Hash
  # Return a new hash with all keys converted to symbols.
  def symbolize_keys
    inject({}) do |options, (key, value)|
      options[key.to_sym] = value
      options
    end
  end

  # Destructively convert all keys to symbols.
  def symbolize_keys!
    keys.each do |key|
      unless key.is_a?(Symbol)
        self[key.to_sym] = self[key]
        delete(key)
      end
    end
    self
  end

  alias_method :to_options,  :symbolize_keys
  alias_method :to_options!, :symbolize_keys!
end