From: dblack@... Date: 2007-08-03T02:32:15+09:00 Subject: Re: binding - how to explain it? Hi -- On Fri, 3 Aug 2007, Giles Bowkett wrote: > There was a thread here recently about this idiom in Rails: > > eval(IO.read(file), binding, file) > > I changed it to a method: > > def eval_file_with_binding(file) > eval(IO.read(file), binding, file) > end > > It appeared twice in Rails, so refactoring it was kind of overkill, > but I have code OCD. Plus, the thread on this list came from somebody > trying to puzzle out what the code did, and it's very repetitive code, > so I wanted to make it simpler and clearer. > > Anyway, the tests pass, but the patch hasn't been applied, because > there's a question raised, which is why does it send the correct > binding when it's defined in some other method. On the one hand the > answer seems obvious, because it's a method on Kernel, but honestly I > can't understand it as well as I think I do, because I can't explain > it beyond that "obvious" point. How do I explain how this code works? > How does this code work? I know it works, but I can't actually explain > it. Can you produce a complete working example? I'm having trouble getting it not to not work.... Here's what I'm doing so far: a = 1 def x eval(DATA.read, binding) end x # error: a undefined __END__ puts a Is that analogous to what's in the original? David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)