# How to do the equivalent of PHP's "$\_GET" in Ruby?

**URL:** <https://www.ruby-forum.com/t/how-to-do-the-equivalent-of-phps-get-in-ruby/137596>\
**Category:** Ruby\
**Created:** [May 14, 2008, 2:53pm UTC](https://www.ruby-forum.com/t/how-to-do-the-equivalent-of-phps-get-in-ruby/137596 "2008-05-14T14:53:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![giantpanda77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e495f1/32.png) [@giantpanda77](https://www.ruby-forum.com/u/giantpanda77)\
**Post date:** [May 14, 2008, 2:53pm UTC](https://www.ruby-forum.com/t/how-to-do-the-equivalent-of-phps-get-in-ruby/137596/1 "2008-05-14T14:53:37Z")

</div>

Hello, all …

I’m new to Ruby, but so far, I like it quite a lot. I’ve a question,  
though … I need to do the Ruby equivalent of PHPs “$\_GET” … how do  
I do this?

Regards,

John

Falling You - exploring the beauty of voice and sound  
[http://www.fallingyou.com](http://www.fallingyou.com)

---

<div class="post-metadata">

**Author:** ![giantpanda77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e495f1/32.png) [@giantpanda77](https://www.ruby-forum.com/u/giantpanda77)\
**Post date:** [May 14, 2008, 3:00pm UTC](https://www.ruby-forum.com/t/how-to-do-the-equivalent-of-phps-get-in-ruby/137596/2 "2008-05-14T15:00:16Z")

</div>

John Z. wrote:

> Hello, all …
> 
> I’m new to Ruby, but so far, I like it quite a lot. I’ve a question,  
> though … I need to do the Ruby equivalent of PHPs “$\_GET” … how do  
> I do this?
> 
> Regards,
> 
> John
> 
> Falling You - exploring the beauty of voice and sound  
> [http://www.fallingyou.com](http://www.fallingyou.com)

is this a Rails question?

in Rails,  
the “params” method called inside your controller  
grabs all of the http params,  
GET or POST.

eg. GET /controller/action/1?question\_id=forsure

> params  
> → {“id” =\> “1”, “question\_id” =\> “forsure”}

---

<div class="post-metadata">

**Author:** ![giantpanda77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e495f1/32.png) [@giantpanda77](https://www.ruby-forum.com/u/giantpanda77)\
**Post date:** [May 14, 2008, 3:03pm UTC](https://www.ruby-forum.com/t/how-to-do-the-equivalent-of-phps-get-in-ruby/137596/3 "2008-05-14T15:03:02Z")

</div>

On Wed, May 14, 2008 at 2:52 PM, John Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello, all …
> 
> I’m new to Ruby, but so far, I like it quite a lot. I’ve a question,  
> though … I need to do the Ruby equivalent of PHPs “$\_GET” … how do I do  
> this?

Please provide context - do you run your script under CGI, FastCGI,  
Mongrel,…?  
For CGI, it’s probably ENV[‘whatever’].

J.

---

<div class="post-metadata">

**Author:** ![giantpanda77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e495f1/32.png) [@giantpanda77](https://www.ruby-forum.com/u/giantpanda77)\
**Post date:** [May 14, 2008, 3:41pm UTC](https://www.ruby-forum.com/t/how-to-do-the-equivalent-of-phps-get-in-ruby/137596/4 "2008-05-14T15:41:38Z")

</div>

Matthew,

Indeed, this is a Rails question … i’m new to this 🙂

My next question is – what is the structure of the params hash i.e.  
how do I access it’s name / value pairs without knowing in advance  
what the name will be? To use your example:

GET /controller/action/1?question\_id=forsure

I want to do something like this:

params.each { | param | do  
url \<\< param[:name] \<\< “=” \<\< param[:value] \<\< “&”  
end

… but I don’t know how to refer to the name / value (I used :name  
and :value here to get the point across).

Regards,

John

Falling You - exploring the beauty of voice and sound  
[http://www.fallingyou.com](http://www.fallingyou.com)

---

<div class="post-metadata">

**Author:** ![giantpanda77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e495f1/32.png) [@giantpanda77](https://www.ruby-forum.com/u/giantpanda77)\
**Post date:** [May 14, 2008, 10:05pm UTC](https://www.ruby-forum.com/t/how-to-do-the-equivalent-of-phps-get-in-ruby/137596/5 "2008-05-14T22:05:01Z")

</div>

John Z. wrote:

> Matthew,
> 
> Indeed, this is a Rails question … i’m new to this 🙂

Rails is a program that someone wrote using the Ruby programming  
language. The program does certain things. Someone may be an expert in  
the Ruby programming language, yet not have any idea what Rails does or  
how it works. How is that possible? Well, suppose you wrote a program  
using Ruby and called it DogCatcher. Would it make sense to come to a  
Ruby forum and ask if anyone can tell you how you can access certain  
data in DogCatcher? Just because someone knows the Ruby programming  
language does not mean they know what every program ever written in Ruby  
does or how it works.

---

<div class="post-metadata">

**Author:** ![giantpanda77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e495f1/32.png) [@giantpanda77](https://www.ruby-forum.com/u/giantpanda77)\
**Post date:** [May 14, 2008, 4:06pm UTC](https://www.ruby-forum.com/t/how-to-do-the-equivalent-of-phps-get-in-ruby/137596/6 "2008-05-14T16:06:20Z")

</div>

On Wed, May 14, 2008 at 3:39 PM, John Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> John

1. Please bottom post (i.e. write your reply under the previous one).  
It’s easier to read.

2. For rails related questions,  
[http://groups.google.com/group/rubyonrails-talk](http://groups.google.com/group/rubyonrails-talk) is more suitable

- you’ll get better/more specific answers there.

Nonetheless, this is a nice ruby question, so here you go:

params.each do | key, value|  
url \<\< key \<\< “=” \<\< value \<\< “&”  
end

Notes: 1. it’s either do…end or {…}, 2. each for Hash takes  
two-parameter block

see [RDoc Documentation](http://ruby-doc.org/core/), class Hash. you’ll find there a lot of  
nice and useful stuff.

For this particular case, Enumerable#map and Array#join are handy:

url = params.map {|key, value| “#{key}=#{value}” }.join(‘&’)

This way you won’t have the trailing &.

J.
