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

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

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

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”}

On Wed, May 14, 2008 at 2:52 PM, John Z. [email protected] 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.

Matthew,

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

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

John Z. wrote:

Matthew,

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

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.

On Wed, May 14, 2008 at 3:39 PM, John Z. [email protected] 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 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 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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs