RCR: Unique Object for Each source file

B

Bill Atkins

Each Ruby .rb file is currently executed within the class Object. So
running "include ModuleX" in your main script file includes the
methods of ModuleX into Object globally. This is messy.

I propose that each file be executed within a new instance of Object.
This way, running include in a file would cause that module to be
included only within the scope of that file.

Bill
 
D

David A. Black

Hi --

Each Ruby .rb file is currently executed within the class Object. So
running "include ModuleX" in your main script file includes the
methods of ModuleX into Object globally. This is messy.

I propose that each file be executed within a new instance of Object.
This way, running include in a file would cause that module to be
included only within the scope of that file.

I'm not seeing the problem here. If you include a module, that module
gets mixed in to whatever module or class called #include. So if it's
Object, you're mixing the module into Object. But usually it isn't,
and in any case it isn't connected with what's in which file.


David
 
R

Robert Klemme

David A. Black said:

.... but might be desired (see below). And it can be avoided, either by
using 'load' with wrap=true or by defining a module in the file and
putting everything there.

IMHO this would break existing code: assume a file containing only method
definitions without any modules, these methods then would not be
available, would they?
I'm not seeing the problem here. If you include a module, that module
gets mixed in to whatever module or class called #include. So if it's
Object, you're mixing the module into Object. But usually it isn't,
and in any case it isn't connected with what's in which file.

And then there is Kernel#load which already allows for wrapping in an
anonymous module:
http://www.ruby-doc.org/docs/rdoc/1.9/classes/Kernel.html#M001574

I'd rather suggest load to return the anonymous instance that was used in
executing the code. That way, one can properly access all those methods
defined in the file.

Regards

robert
 
G

Guillaume Marcais

Le 11 mai 04, à 07:38, Robert Klemme a écrit :
And then there is Kernel#load which already allows for wrapping in an
anonymous module:
http://www.ruby-doc.org/docs/rdoc/1.9/classes/Kernel.html#M001574

I'd rather suggest load to return the anonymous instance that was used
in
executing the code. That way, one can properly access all those
methods
defined in the file.

And what about:
def load(file, wrap=false)
case wrap
when Module: # Wrap in this module
when true: # Wrap in an anonymous module and return it
else # Load normally
end

Guillaume.
 
R

Robert Klemme

Guillaume Marcais said:
Le 11 mai 04, à 07:38, Robert Klemme a écrit :

And what about:
def load(file, wrap=false)
case wrap
when Module: # Wrap in this module
when true: # Wrap in an anonymous module and return it
else # Load normally
end

Why doesn't this do the same trick?

SomeModule.instance_eval do
load 'thefile.ext'
end

Can anyone with a bit more insight than me shed some light on this? This
works:

Foo.instance_eval do
eval File.read('mod.rb')
end

Foo.instance_eval File.read('mod.rb')

14:48:46 [ruby]: ./load_mod2.rb
MAIN in main id=134728276 class=Object
Loading... in Foo id=134689816 class=Module
Loading... in Foo id=134689816 class=Module
14:49:50 [ruby]: cat load_mod2.rb
#!/usr/bin/ruby

puts "MAIN in #{inspect} id=#{id} class=#{self.class.inspect}"

module Foo
end

Foo.instance_eval do
eval File.read('mod.rb')
end

Foo.instance_eval File.read('mod.rb')
14:49:53 [ruby]: cat mod.rb
#!/usr/bin/ruby

puts "Loading... in #{inspect} id=#{id} class=#{self.class.inspect}"

Kind regards

robert
 
N

nobu.nokada

Hi,

At Tue, 11 May 2004 21:53:53 +0900,
Robert Klemme wrote in [ruby-talk:99840]:
Why doesn't this do the same trick?

SomeModule.instance_eval do
load 'thefile.ext'
end

Loaded file is evaluated in its context, not in loader's
context.

Therefore, require/load can take effect globally even if it is
in a class statement.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top