Class: Hash
- Object
- Hash
encoding: utf-8
Public Visibility
Public Instance Method Summary
#deep_symbolize_keys |
Return duplication of self with all keys recursively converted to symbols. Returns: Hash |
---|---|
#deep_symbolize_keys! |
Recursively replace keys in self by coresponding symbols. Returns: Hash |
#extract!(*args) |
Returns the value of self for each argument and deletes those entries. |
#get(*keys) |
Simplier syntax for code such as params[:post] && params[:post][:title]. Returns: Object |
#reverse_merge(another) |
Returns: String |
#reverse_merge!(another) |
Returns: String |
#symbolize_keys |
Return duplication of self with all keys non-recursively converted to symbols. Returns: Hash |
#symbolize_keys! |
Replace keys in self by coresponding symbols. Returns: Hash |
#to_html_attrs |
Returns: String |
#to_native | |
#to_url_attrs |
Returns: String |
Public Instance Methods Inherited from Object
Public Instance Method Details
deep_symbolize_keys
Return duplication of self with all keys recursively converted to symbols
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rango/ext/hash.rb', line 30 def deep_symbolize_keys self.inject(Hash.new) do |result, array| key, value = array.first, array.last if value.respond_to?(:symbolize_keys) result[key.to_sym] = value.symbolize_keys else result[key.to_sym] = value end result end end |
deep_symbolize_keys!
Recursively replace keys in self by coresponding symbols
47 48 49 |
# File 'lib/rango/ext/hash.rb', line 47 def deep_symbolize_keys! self.replace(self.deep_symbolize_keys) end |
extract!
Returns the value of self for each argument and deletes those entries.
62 63 64 65 66 |
# File 'lib/rango/ext/hash.rb', line 62 def extract!(*args) args.map do |arg| self.delete(arg) end end |
get
Simplier syntax for code such as params[:post] && params[:post][:title]
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/rango/ext/hash.rb', line 130 def get(*keys) raise ArgumentError, "You must specify at least one key" if keys.empty? keys.inject(self) do |object, key| # Hash#fetch works similar as Hash#[], but [] method is # defined for too many objects, also strings and even numbers if object.respond_to?(:fetch) begin object.fetch(key) # Hash#fetch raise IndexError if key doesn't exist rescue IndexError return nil end else raise IndexError, "Object #{object.inspect} isn't hash-like collection" end end end |
reverse_merge
89 90 91 |
# File 'lib/rango/ext/hash.rb', line 89 def reverse_merge(another) another.merge(self) end |
reverse_merge!
96 97 98 |
# File 'lib/rango/ext/hash.rb', line 96 def reverse_merge!(another) self.replace(self.reverse_merge(another)) end |
symbolize_keys
Return duplication of self with all keys non-recursively converted to symbols
9 10 11 12 13 14 |
# File 'lib/rango/ext/hash.rb', line 9 def symbolize_keys self.inject(Hash.new) do |result, array| result[array.first.to_sym] = array.last result end end |
symbolize_keys!
Replace keys in self by coresponding symbols
21 22 23 |
# File 'lib/rango/ext/hash.rb', line 21 def symbolize_keys! self.replace(self.symbolize_keys) end |
to_html_attrs
73 74 75 |
# File 'lib/rango/ext/hash.rb', line 73 def to_html_attrs self.map { |key, value| "#{key}='#{value}'" }.join(" ") end |
to_native
Deprecated.
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/rango/ext/hash.rb', line 103 def to_native self.each do |key, value| value = case value when "true" then true when "false" then false when "nil" then nil when /^\d+$/ then value.to_i when /^\d+\.\d+$/ then value.to_f else value end self[key.to_sym] = value end return self end |
to_url_attrs
82 83 84 |
# File 'lib/rango/ext/hash.rb', line 82 def to_url_attrs self.map { |key, value| "#{key}=#{value}" }.join("&") end |