GET-parameters an local vars

Discussion in 'Ruby' started by Dirk Einecke, May 15, 2004.

  1. Dirk Einecke

    Dirk Einecke Guest

    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
    Dirk Einecke, May 15, 2004
    #1
    1. Advertising

  2. Dirk Einecke

    Ceri Storey Guest

    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 <>
    Ceri Storey, May 15, 2004
    #2
    1. Advertising

  3. 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
    Florian Gross, May 15, 2004
    #3
  4. Dirk Einecke

    Dirk Einecke Guest

    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
    Dirk Einecke, May 15, 2004
    #4
  5. Dirk Einecke

    Dirk Einecke Guest

    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)
    Dirk Einecke, May 15, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon

    app vars and cache vars

    Jon, Dec 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    392
  2. Linuxguy123
    Replies:
    7
    Views:
    685
    Paddy O'Loughlin
    Feb 20, 2009
  3. caccolangrifata
    Replies:
    18
    Views:
    393
    Chris Torek
    Jul 22, 2011
  4. Replies:
    0
    Views:
    93
  5. Raymond O'connor

    testing local vars and exceptions

    Raymond O'connor, Jan 31, 2007, in forum: Ruby
    Replies:
    1
    Views:
    92
    Pit Capitain
    Jan 31, 2007
Loading...

Share This Page