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

J

John Zorko

[Note: parts of this message were removed to make it a legal post.]


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
 
M

Matthew Rudy Jacobs

John said:
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
-> {"id" => "1", "question_id" => "forsure"}
 
J

Jano Svitok

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.
 
J

John Zorko

[Note: parts of this message were removed to make it a legal post.]


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
 
J

Jano Svitok

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

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

7stud --

John said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,124
Latest member
JuniorPell
Top