On Tuesday 09 July 2002 05:10 pm, Philip Mak wrote:
> Observe the following piece of
> code, which looks like it should work but doesn't:
>
>
> $ ruby -e"eval 'x = 3'; puts x"
> -e:1: undefined local variable or method `x' for
> #<Object:0x401ddce0> (NameError)
>
>
> Does anyone have ideas on how I could create such a function, or am
> I trying to do the impossible? I want to minimize the amount of
> code that has to be typed by the caller.

class CGI < Hash
  def initialize
    self['name']=['fred', nil]
    self['email']=['joe / pete.com', nil]
    self['phone']=['000-0000', nil]
  end
  def export(toWhom, names)
    toWhom.instance_eval("class <<self; attr_accessor :_mycgi; end")
    toWhom._mycgi = self
    names.each { |name|
      toWhom.instance_eval("def self.#{name};  _mycgi['#{name}'][0]; 
end")
    }
  end
end

class SomeOtherClass
  def testMethod
    cgi = CGI.new
    cgi.export(self, %w(name email phone))
    p name
    p email
    p phone
  end
end

s = SomeOtherClass.new
s.testMethod

-- 
Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE