Forum: Ruby Building querystrings with ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
94846d7255b19629c0c78700d9b5a046?d=identicon&s=25 ngw (Guest)
on 2006-03-15 01:27
(Received via mailing list)
Hi *, my question is really simple: is there a way in Ruby stdlib or in
external modules to build querystrings without creating the string from
scratch ?
For example something like:

name = "Foo Bar"
p URI::join(base_uri, query_string(name))

-> "http://foobar.org/?name=Foo+Bar"

Obviously this code is totally invented.
If not (haven't found anything in the documentation) how does people
accomplish this task ?

TIA,
  ngw
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-03-15 03:35
(Received via mailing list)
On Wed, 15 Mar 2006, ngw wrote:

> Obviously this code is totally invented.
> If not (haven't found anything in the documentation) how does people
> accomplish this task ?
>
> TIA,
>  ngw

   harp:~ > cat a.rb
   require "cgi"
   class Hash
     def query_string
       "?" << inject([]){|a,kv| a << [CGI.escape(kv.shift),
CGI.escape(kv.shift)].join("=") }.join("&")
     end
   end

   q = { "name" => "Foo Bar", "uri" => "http://ruby-lang.org" }

   p q.query_string

   harp:~ > ruby a.rb
   "?name=Foo+Bar&uri=http%3A%2F%2Fruby-lang.org"

regards.

-a
2abf5beb51d5d66211d525a72c5cb39d?d=identicon&s=25 Paul Battley (Guest)
on 2006-03-15 10:09
(Received via mailing list)
On 15/03/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:
>    require "cgi"
>    class Hash
>      def query_string
>        "?" << inject([]){|a,kv| a << [CGI.escape(kv.shift), 
CGI.escape(kv.shift)].join("=") }.join("&")
>      end
>    end

Now here's somewhere I can use the handy trick I learned yesterday:

"?" << inject([]){ |a,(k,v)| a << [CGI.escape(k),
CGI.escape(v)].join("=") }.join("&")

But this is shorter:

"?" << inject([]){ |a,kv| a << kv.map{ |e| CGI.escape(e) }.join("=")
}.join("&")

And this is shorter still:

"?" << to_hash.map{ |kv| kv.map{ |e| CGI.escape(e) }.join("=")
}.join("&")

Paul.
2abf5beb51d5d66211d525a72c5cb39d?d=identicon&s=25 Paul Battley (Guest)
on 2006-03-15 10:12
(Received via mailing list)
On 15/03/06, Paul Battley <pbattley@gmail.com> wrote:
> And this is shorter still:
>
> "?" << to_hash.map{ |kv| kv.map{ |e| CGI.escape(e) }.join("=") }.join("&")

Ignore my stupidity above; this is shorter and less redundant:

"?" << map{ |kv| kv.map{ |e| CGI.escape(e) }.join("=") }.join("&")

Paul.
94846d7255b19629c0c78700d9b5a046?d=identicon&s=25 ngw (Guest)
on 2006-03-15 11:20
(Received via mailing list)
-Paul Battley <pbattley@gmail.com>:
> On 15/03/06, Paul Battley <pbattley@gmail.com> wrote:
>> And this is shorter still:
>>
>> "?" << to_hash.map{ |kv| kv.map{ |e| CGI.escape(e) }.join("=") }.join("&")
>
> Ignore my stupidity above; this is shorter and less redundant:
>
> "?" << map{ |kv| kv.map{ |e| CGI.escape(e) }.join("=") }.join("&")

Wow !
Thank you both, great snippets.

  ngw
7615a81e7688cccd95e66fc8df44991b?d=identicon&s=25 ebeard (Guest)
on 2006-03-16 15:10
(Received via mailing list)
Wow, I have no idea what this does or how it works.  Would you mind
taking this apart and describing how it works?
2abf5beb51d5d66211d525a72c5cb39d?d=identicon&s=25 Paul Battley (Guest)
on 2006-03-16 17:28
(Received via mailing list)
On 16/03/06, ebeard <ecbearden@gmail.com> wrote:
> Wow, I have no idea what this does or how it works.  Would you mind
> taking this apart and describing how it works?

  "?"<<map{|h|h.map{|e|CGI.escape(e)}*'='}*'&'

This can be spaced out a bit:

  "?" <<              # append what follows to the string '?'
  map { |h|           # h looks like ['key', 'value'] for each key-value
pair
    h.map { |e|       # e looks like 'key' or 'value'
      CGI.escape(e)   # escape each element
    } * '='           # join key and value with '='
  } * '&'             # join each key-value pair with '&'

Is that any clearer?

Paul.
7615a81e7688cccd95e66fc8df44991b?d=identicon&s=25 ebeard (Guest)
on 2006-03-22 16:34
(Received via mailing list)
perfectly.  very clever.

thanks,

chad.
This topic is locked and can not be replied to.