From: "David A. Black" Date: 2005-10-10T11:48:56+09:00 Subject: Re: local_variable_set ? Hi -- On Mon, 10 Oct 2005, David A. Black wrote: > Hi -- > > On Mon, 10 Oct 2005, Hal Fulton wrote: > >> No, this isn't April Fool's Day. I'm seriously wondering >> if this has ever been considered. >> >> I can only think of one place I'd use it -- to turn hashes >> (used as named parameters) into real variables. >> >> Something like: >> >> def meth(hash) >> bind = binding >> hash.each_pair {|k,v| local_variable_set(k,v,bind) } >> # Assuming call was: meth(alpha: 123, beta: 234, gamma: 345) >> # we now can reference alpha, beta, and gamma as ordinary >> # variables... >> end > > I think making bindings writeable has been suggested: > > binding[:alpha] = 123 > > or whatever. > >> Thoughts? > > To me it suggests too tight a coupling between the caller and the > method. If you write a method that depends on being told to create a > local variable with a particular name, you're limiting what can be > done by way of refactoring and reimplementing the method. Maybe not, though. I guess the idea would be that the caller is supposed to send those names in a hash anyway. I think I might find it weird, though, to see: def meth(hash) bind = binding hash.each_pair {|k,v| local_variable_set(k,v,bind) } puts alpha beta += 2 if defined?(gamma) puts "gamma came from somewhere..." etc., without having any visual cues (arglist or assignment) as to where those names came from. David -- David A. Black dblack@wobblini.net