# Converting hash to option string

**URL:** <https://www.ruby-forum.com/t/converting-hash-to-option-string/183647>\
**Category:** Rails\
**Created:** [February 18, 2010, 4:50am UTC](https://www.ruby-forum.com/t/converting-hash-to-option-string/183647 "2010-02-18T04:50:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [February 18, 2010, 4:50am UTC](https://www.ruby-forum.com/t/converting-hash-to-option-string/183647/1 "2010-02-18T04:50:16Z")

</div>

Is there a good way to programmatically convert  
{:a=\>‘b’, :c=\>‘d’}  
to  
‘a=b c=d’  
?

Rails seems to do this knid of thing “all over the place”.

I see reverse\_merge and similar functions … but I wonder if this is  
canned behavior somewhere.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [February 18, 2010, 6:27am UTC](https://www.ruby-forum.com/t/converting-hash-to-option-string/183647/2 "2010-02-18T06:27:18Z")

</div>

On Feb 17, 9:50 pm, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there a good way to programmatically convert  
> {:a=\>‘b’, :c=\>‘d’}  
> to  
> ‘a=b c=d’  
> ?
> 
> Rails seems to do this knid of thing “all over the place”.

I think you’re looking for Hash#to\_query:

h = { :a =\> ‘b’, :c =\> ‘d’ }  
opts = h.to\_query # =\> “a=b&c=d”

Jeff  
[purpleworkshops.com](http://purpleworkshops.com)

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [February 18, 2010, 1:14pm UTC](https://www.ruby-forum.com/t/converting-hash-to-option-string/183647/3 "2010-02-18T13:14:55Z")

</div>

> I think you’re looking for Hash#to\_query:
> 
> h = { :a =\> ‘b’, :c =\> ‘d’ }  
> opts = h.to\_query # =\> “a=b&c=d”

Jeff, thanks, but no cigar.

But you got me real close and you got me to a solution.

Here’s my solution which is, basically, a rip off of  
activesupport-2.3.5\lib\active\_support\core\_ext\array\conversions.rb  
activesupport-2.3.5\lib\active\_support\core\_ext\hash\conversions.rb  
activesupport-2.3.5\lib\active\_support\core\_ext\hobject\conversions.rb

* * *

class Array  
def to\_html\_parms(key)  
prefix = “#{key}[]”  
collect { |value| value.to\_html\_parms(prefix) }.join ’ ’  
end  
end

class Hash  
def to\_html\_parms(namespace = nil)  
collect do |key, value|  
value.to\_html\_parms(namespace ? “#{namespace}[#{key}]” : key)  
end.sort \* ’ ’  
end  
end

class Object  
def to\_html\_parms(key)  
require ‘cgi’ unless defined?(CGI) && defined?(CGI::escape)  
“#{CGI.escape(key.to\_s)}=#{CGI.escape(to\_param.to\_s)}”  
end  
end

* * *

x={:ralph =\> ‘marti’, :steve =\> ‘heller’}  
x.to\_html\_parms # =\> “ralph=marti steve=heller”

* * *

Now … could someone explain to me what the cgi stuff does??

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [February 18, 2010, 1:32pm UTC](https://www.ruby-forum.com/t/converting-hash-to-option-string/183647/4 "2010-02-18T13:32:13Z")

</div>

On 18 February 2010 12:14, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Now … could someone explain to me what the cgi stuff does??

[http://ruby-doc.org/core/classes/CGI.html](http://ruby-doc.org/core/classes/CGI.html)

The thing is, you’ve not said what your problem is, only asked for a  
way do what you think is the way to solve your problem.

to turn a hash into a string of “key=value” for each pair separated by  
a space (which is what you’ve asked for), you could solve a dozen ways  
(without extending base ruby classes).

For instance, this seems to do what you want:  
{ :a =\> ‘b’, :c =\> ‘d’ }.inject([]) {|output, pair| output \<\<  
“#{pair[0]}=#{pair[1]}”}.join(" ")

but it’s quite smelly code, and doesn’t account for potential  
confusion that could occur to your output in the event that the hash  
was comprised thus:

{ :a =\> ‘b e=f’, :c =\> ‘d a=c’ }

Explain your _problem_, ie: what is it that’s causing you to think  
that that hash has to turn into a string; and you may find out that  
your approach isn’t the most effective, and that someone suggests a  
more elegant/efficient/robust way to approach it.

Regards,  
Michael

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [February 18, 2010, 3:42pm UTC](https://www.ruby-forum.com/t/converting-hash-to-option-string/183647/5 "2010-02-18T15:42:11Z")

</div>

On Feb 18, 2:30 pm, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> x={:summary=\>“Standard alerts box”, :class=“alerts”, :cellspacing=“0”}

content\_tag can do that for you:

\<% content\_tag ‘table’, :class =\> ‘alerts’, :cellspacing =\> ‘0’ do %\>

\<% end %\>

Fred

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [February 18, 2010, 3:30pm UTC](https://www.ruby-forum.com/t/converting-hash-to-option-string/183647/6 "2010-02-18T15:30:27Z")

</div>

Michael P. wrote:

> Explain your _problem_, ie: what is it that’s causing you to think  
> that that hash has to turn into a string; and you may find out that  
> your approach isn’t the most effective, and that someone suggests a  
> more elegant/efficient/robust way to approach it.
> 
> Regards,  
> Michael

I am creating HTML and want to do something like

x={:summary=\>“Standard alerts box”, :class=“alerts”, :cellspacing=“0”}

so that

 \>

produces
