Converting hash to option string

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.

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

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??

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

Michael Pavling 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

  <table <%= x.to_html %> >

produces

  <table 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