"Dirk Einecke" <dirk.einecke / gmx.de> schrieb im Newsbeitrag
news:2lt74lFgkgt1U1 / uni-berlin.de...
> Hi.
>
> I've a class to get all vars from params:
>
> class CgiParamsToLocal
>    def initialize(params)
>      @params = params
>    end
>    def method_missing(m, *other)
>      @params[m.to_s][0]
>    end
> end
>
> Using:
>
> p = CgiParamsToLocal.new($cgi.params)
>
> No I want to check if for example p.foo ist defined. I tried it like this:
>
> print p.foo # -> Test
> if ( defined?(p.foo) ) then
>    # do something
> else
>    # do some other things
> end
>
> Well - p.foo is defined but why I ever get only the else part?

Where is that defined?  You don't have a method #foo in CgiParamsToLocal and
you don't create it when the instance is created.  You have to invoke #foo
(the method that is missing) in order to get the value from #method_missing
back.

> If I do it like this all is okay:
>
> print p.foo # -> Test
> if ( p.foo.length != 0 ) then
>    # do something
> else
>    # do some other things
> end
>
> Now - why is defined? not working in my example?

Usually people coming grom Perl use defined? the wrong way.  You typically
want "something.nil?" or just "something":

if p.foo
  # foo set
else
  # foo unset
end

or

if p.foo.nil?
  # unset
else
  # set
end

Does that help?

Regards

    robert