From: Robert Klemme Date: 2006-02-05T03:53:19+09:00 Subject: Re: Extract hash into local variables? Jeffrey Schwab wrote: > csn wrote: >> Is there a Ruby function similar to PHP's extract/list? What I'd >> like to do is: >> >> def foo(args={a=>1, b=>2, c=>3}) >> args.extract >> >> puts a >> puts b >> puts c >> end > > I'm kind of disturbed by how hard this actually seems. There does not > seem to be any decent way to modify the set of local variables through > introspection. Here's a weak work-around: > > > def foo(args={:a=>1, :b=>2, :c=>3}) > @args = args > > def method_missing(m) > @args[m] > end > > puts a > puts b > puts c > > end Very weak. This is by no means thread safe. Also it will keep references to the arguments around after the method terminates. Plus, you're redefining method_missing all the time. Too many drawbacks IMHO. I don't know why people strive to get local variables set dynamically. If you want to use them, you'll have to make them explicitely anyway. So what do we gain? Is there a real world problem that can't be solved without this? Cheers robert