From: Adam Prescott Date: 2011-05-28T00:57:29+09:00 Subject: Re: Method that mutates object --90e6ba6e83d8bbab7004a443fd38 Content-Type: text/plain; charset=UTF-8 On 26 May 2011 22:40, "Gary Wright" wrote: > > There are a variety of ways for a Ruby method to gain access to variable bindings that are out of the direct scope of the method but it is generally considered bad form for a method to alter variable bindings outside its scope. Nevertheless, here is one way to do it: > > class Integer > def assign(name, value, binding_object) > eval "#{name} = #{value}", binding_object > end > end > > >> x = 5 #=> 5 > >> x.assign('x', 7, binding) #=> 7 > >> x #=> 7 > > This sort of use of eval and binding is *not* at all common and probably only comes into play with various debugging or programming tools rather than in the normal course of writing Ruby programs. Not to needlessly self-promote or anything, but I recently wrote about variables and bindings, so perhaps this is informative: http://aprescott.com/posts/variables-closures-and-scope --90e6ba6e83d8bbab7004a443fd38--