Class: Hash
Instance Method Summary collapse
-
#-(ars) ⇒ Object
Deletes the key(s) passed in from the hash.
- #join(pair_string, join_string) ⇒ Object
-
#to_params(escape = true) ⇒ Object
Converts a hash to query string parameters.
Instance Method Details
#-(ars) ⇒ Object
Deletes the key(s) passed in from the hash.
14 15 16 17 |
# File 'lib/extensions/hash.rb', line 14 def -(ars) [ars].flatten.each {|a| self.delete(a)} self end |
#join(pair_string, join_string) ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'lib/extensions/hash.rb', line 4 def join(pair_string, join_string) output = [] sorted = self.sort {|a,b| a[0].to_s <=> b[0].to_s} sorted.each do |ar| output << sprintf(pair_string, ar[0], ar[1]) end output.join(join_string) end |
#to_params(escape = true) ⇒ Object
Converts a hash to query string parameters. An optional boolean escapes the values if true, which is the default.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/extensions/hash.rb', line 21 def to_params(escape = true) params = '' stack = [] each do |k, v| if v.is_a?(Hash) stack << [k,v] else v = v.to_s.uri_escape if escape params << "#{k}=#{v}&" end end stack.each do |parent, hash| hash.each do |k, v| if v.is_a?(Hash) stack << ["#{parent}[#{k}]", v] else v = v.to_s.uri_escape if escape params << "#{parent}[#{k}]=#{v}&" end end end params.chop! # trailing & params.split("&").sort.join("&") end |