From: Dan Doel Date: 2004-04-21T21:53:02+09:00 Subject: Re: Ruby proposal: MAIN_BINDING On Wednesday 21 April 2004 7:09 am, Szymon Drejewicz wrote: > What do you think about MAIN_BINDING for use it with eval(): > > eval("a = 'hello'",MAIN_BINDING) > puts a > > outputs: > hello Strictly speaking, there is no binding that would allow you to define new variables in the main scope with the current semantics of #eval. The issue (as I understand it), is that #eval opens up a new scope, sort of like a block currently does, and variables don't leak out of it. For example: 5.times do |i| j = i end puts j In Ruby 1.8 this fails because j isn't defined (in Ruby 2.0, this won't fail). #eval is similar. It opens up a new scope that can only be accessed through #eval, so you'd have to change the method itself. Strictly speaking, I guess you could have a boolean argument that specifies whether #eval should use the inner scope. That'd be more general. Do I think it's a good idea? I don't know. :) I recall reading threads here where that kind of thing was necessary for what the person wanted to do (like in your example. You can't define new variables in the general scope). I don't really know how common it is, though. On the other hand, I don't know how much you need to protect the main scope. Perhaps #eval should be changed to let variables leak out of eval-scope, just like they'll leak out of blocks in the future. - Dan