File: hash_helper.rb

package info (click to toggle)
ruby-twitter-text 1.14.7%2Bconformance-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 772 kB
  • ctags: 219
  • sloc: ruby: 2,917; java: 1,571; makefile: 6
file content (21 lines) | stat: -rw-r--r-- 633 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
module Twitter
  module HashHelper
    # Return a new hash with all keys converted to symbols, as long as
    # they respond to +to_sym+.
    #
    #   { 'name' => 'Rob', 'years' => '28' }.symbolize_keys
    #   #=> { :name => "Rob", :years => "28" }
    def self.symbolize_keys(hash)
      symbolize_keys!(hash.dup)
    end

    # Destructively convert all keys to symbols, as long as they respond
    # to +to_sym+. Same as +symbolize_keys+, but modifies +self+.
    def self.symbolize_keys!(hash)
      hash.keys.each do |key|
        hash[(key.to_sym rescue key) || key] = hash.delete(key)
      end
      hash
    end
  end
end