# Help - Hash.to\_params

**URL:** <https://www.ruby-forum.com/t/help-hash-to-params/84951>\
**Category:** Rails\
**Created:** [February 8, 2007, 8:37pm UTC](https://www.ruby-forum.com/t/help-hash-to-params/84951 "2007-02-08T20:37:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![technodolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e99b99/32.png) [@technodolt](https://www.ruby-forum.com/u/technodolt)\
**Post date:** [February 8, 2007, 8:37pm UTC](https://www.ruby-forum.com/t/help-hash-to-params/84951/1 "2007-02-08T20:37:45Z")

</div>

This is my current implementation for Hash.to\_params

If you have spare time, please help me optimize this.

Thanks.

class Hash  
def to\_params(parent = ‘’)  
ret = ‘’  
self.keys.each do |key|  
if self[key].is\_a? Hash  
if parent == ‘’  
ret += self[key].to\_uri(key.to\_s)  
else  
ret += self[key].to\_uri(parent + “[#{key.to\_s}]”)  
end  
else  
if parent == ‘’  
ret += “#{key}=#{self[key]}&”  
else  
ret += “#{parent}[#{key}]=#{self[key]}&”  
end  
end  
end  
return ret.chomp(’&’)  
end  
end

---

<div class="post-metadata">

**Author:** ![technodolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e99b99/32.png) [@technodolt](https://www.ruby-forum.com/u/technodolt)\
**Post date:** [February 8, 2007, 8:49pm UTC](https://www.ruby-forum.com/t/help-hash-to-params/84951/2 "2007-02-08T20:49:15Z")

</div>

what do you plan to use this for?

as it would seem url\_for() would do just fine

---

<div class="post-metadata">

**Author:** ![technodolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e99b99/32.png) [@technodolt](https://www.ruby-forum.com/u/technodolt)\
**Post date:** [February 8, 2007, 10:27pm UTC](https://www.ruby-forum.com/t/help-hash-to-params/84951/3 "2007-02-08T22:27:03Z")

</div>

Keynan P. wrote:

> what do you plan to use this for?
> 
> as it would seem url\_for() would do just fine

How do I test url\_for in script/console? I’m not exactly certain how it  
works, and I’d like to test and see if it does what I want.

Thanks.
