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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 17, 9:50 pm, Ralph Shnelvar <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> 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?? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 18 February 2010 12:14, Ralph Shnelvar <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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, > MichaelI 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"> -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 18, 2:30 pm, Ralph Shnelvar <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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"} >content_tag can do that for you: <% content_tag ''table'', :class => ''alerts'', :cellspacing => ''0'' do %> <% end %> Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.