Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > GET-parameters an local vars

Reply
Thread Tools

GET-parameters an local vars

 
 
Dirk Einecke
Guest
Posts: n/a
 
      05-15-2004
Hi.

I call i a file like this: index.rb?foo=test1&bar=test2
Now I want all given parameters as local vars:

foo = cgi.params['foo'][0]
bar = cgi.params['bar'][0]

Is there a faster way for example with a loop to write all given
parameters in local vars?

greetings
Dirk Einecke
 
Reply With Quote
 
 
 
 
Ceri Storey
Guest
Posts: n/a
 
      05-15-2004
On Sun, May 16, 2004 at 04:03:50AM +0900, Dirk Einecke wrote:
> Hi.


> Is there a faster way for example with a loop to write all given
> parameters in local vars?


Well, you could do something with eval allong the following lines, but
It's a rather Bad Idea.

def setup(params, binding)
params.each do |key, val|
eval("%s = %s" % [key, val[0]], binding)
end
end

# the call to Kernel#binding method is the key here, since it allows you
# to assign to the variables within the correct scope.
setup(cgi.params, binding)

A cleaner solution might well be to use a proxy object, which makes use
of the special method, method_missing to map keys to values.

class CgiParam
def initialize(params)
@params = params
end
def method_missing(m, *other)
@params[m.to_s][0]
end
end

p = CgiParam.new(cgi.params)

.. if (p.foo) # do things
... unless (p.bar.to_i) # other things


Although there may still be a better way of doing it.
--
Ceri Storey <(E-Mail Removed)>


 
Reply With Quote
 
 
 
 
Florian Gross
Guest
Posts: n/a
 
      05-15-2004
Dirk Einecke wrote:

> Hi.


Moin!

> Is there a faster way for example with a loop to write all given
> parameters in local vars?


This code might help:

arg = Module.new do
extend self
define_method(:method_missing) do |name|
(cgi.params[name.to_s] || []).first
end
end

It's used like this:

arg.one # => 'eins'
arg::two # => 'zwei'

You can even do this if you want PHP-style auto import, but I heavily
suggest not doing this: (Huge security traps are caused by it!)

include arg
three # => 'drei'

> greetings
> Dirk Einecke


Regards,
Florian Gross
 
Reply With Quote
 
Dirk Einecke
Guest
Posts: n/a
 
      05-15-2004
Hi Ceri,

Ceri Storey wrote:
> A cleaner solution might well be to use a proxy object, which makes use
> of the special method, method_missing to map keys to values.
>
> class CgiParam
> def initialize(params)
> @params = params
> end
> def method_missing(m, *other)
> @params[m.to_s][0]
> end
> end
>
> p = CgiParam.new(cgi.params)
>
> . if (p.foo) # do things
> ... unless (p.bar.to_i) # other things


This sounds good. I will try this...
Thanks.

greetings
Dirk Einecke
 
Reply With Quote
 
Dirk Einecke
Guest
Posts: n/a
 
      05-15-2004
Hi Florian,

Thank you for you posting. Sound good too - but first I will try the way
Ceri shows with the class.

greetings (to germany?)
Dirk Einecke (from germany - Karlsruhe)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use self.vars in class.method(parameters, self.vars) caccolangrifata Python 18 07-22-2011 10:22 PM
How do I declare global vars or class vars in Python ? Linuxguy123 Python 7 02-20-2009 06:45 PM
testing local vars and exceptions Raymond O'connor Ruby 1 01-31-2007 11:34 AM
local vars with punctuation listrecv@gmail.com Ruby 0 04-27-2006 04:27 PM
app vars and cache vars Jon ASP .Net 3 12-14-2004 08:52 PM



Advertisments