Markus Fischer wrote:
> On 29.08.2010 12:40, Robert Klemme wrote:
>> On 29.08.2010 12:25, Ammar Ali wrote:
>>> Are there any gotchas in requiring files from within a method? It
>>> seems to work, but I have a sinking feeling that I'm missing something
>>> here.
>> Not technically.  It might even be more efficient if you require a
>> particular functionality only rarely and include the "require" in the
>> method that needs that functionality.
> Sorry for hijacking, but I'm wondering how require exactly works in the
> context of a class or method; does scoping play any role if I've defined
> classes/modules in the required file?

No. Required files always get evaluated in the global context. Which
is actually another gotcha: if you call require inside a module or
class, then it *looks like* you jail the library into a namespace,
when in fact you don't. Code that looks like it's doing one thing, but
does another thing, is always dangerous.

Note that Kernel#load takes an optional boolean argument telling it to
load the file into an anonymous module instead of the global
namespace. (Unfortunately, there doesn't seem to be any way to
actually *get* at that module[1], so this API looks rather useless.
Also, the code in that file can always use ::Object to gain access to
the global namespace.)

jwm

[1] except for ugly hacks with ObjectSpace