Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/chirpy.rb

Overview

Adds to_url_params and from_url_params methods to the Hash class. I found the code from: www.ruby-forum.com/topic/69428

Instance Method Summary collapse

Instance Method Details

#from_url_params(url_params) ⇒ Object

Takes a string of URL parameters and turns them into a hash



21
22
23
24
25
26
27
28
# File 'lib/chirpy.rb', line 21

def from_url_params(url_params)
  result = {}.with_indifferent_access
  url_params.split('&').each do |element|
    element = element.split('=')
    result[element[0]] = element[1]
  end
  result
end

#to_url_paramsObject

Turns a hash into URL parameters, e.g. “key=value&another_key=another_value”



12
13
14
15
16
17
18
# File 'lib/chirpy.rb', line 12

def to_url_params
  elements = []
  keys.size.times do |i|
    elements << "#{keys[i]}=#{values[i]}"
  end
  elements.join('&')
end