[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

John Zorko

5/14/2008 12:52:00 PM

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












5 Answers

Matthew Rudy Jacobs

5/14/2008 1:00:00 PM

0

John Zorko 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.fall...

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"}
--
Posted via http://www.ruby-....

Jano Svitok

5/14/2008 1:01:00 PM

0

On Wed, May 14, 2008 at 2:52 PM, John Zorko <jmzorko@mac.com> 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.

John Zorko

5/14/2008 1:39:00 PM

0

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











On May 14, 2008, at 6:00 AM, Matthew Rudy Jacobs wrote:

> John Zorko 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.fall...
>
> 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"}
> --
> Posted via http://www.ruby-....
>


Jano Svitok

5/14/2008 2:05:00 PM

0

On Wed, May 14, 2008 at 3:39 PM, John Zorko <jmzorko@mac.com> wrote:
>
> 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/rubyon... 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..., 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.

7stud --

5/14/2008 8:05:00 PM

0

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

--
Posted via http://www.ruby-....