Wrap external source file in a module

A

Alexey Muranov

Hello, i have another question.

I have a strange situation which i have not resolved yet where for some
reason i have to wrap several classes in a module, otherwise the program
doesn't work as expected (by me of course).
Can anybody please tell me the following: is it possible to read class
definition from another file and wrap them in a module?

Here is an example:

I have several classes defined in a file models.rb:

class Person
...
end

class Address
...
end

I want to load or require this file in another file, say program.rb, so
that the classes were wrapped together in a module M, like

module M
load "models.rb"
end

p = M::person.new

but this doesn't work (M::person is an uninitialized constant, according
to an error message).

Is there some way to do this?

Thank you,

Alexey.
 
I

Intransition

Hello, i have another question.

I have a strange situation which i have not resolved yet where for some
reason i have to wrap several classes in a module, otherwise the program
doesn't work as expected (by me of course).
Can anybody please tell me the following: is it possible to read class
definition from another file and wrap them in a module?

Here is an example:

I have several classes defined in a file models.rb:

=A0 =A0 class Person
=A0 =A0 =A0 ...
=A0 =A0 end

=A0 =A0 class Address
=A0 =A0 =A0 ...
=A0 =A0 end

I want to load or require this file in another file, say program.rb, so
that the classes were wrapped together in a module M, like

=A0 =A0 module M
=A0 =A0 =A0 load "models.rb"
=A0 =A0 end

=A0 =A0 p =3D M::person.new

but this doesn't work (M::person is an uninitialized constant, according
to an error message).

Is there some way to do this?

You need to evaluate the file:

=A0 =A0 module M
=A0 =A0 =A0 module_eval File.read(File.dirname(__FILE__) + '/models.rb')
=A0 =A0 end

=A0 =A0 p =3D M::person.new
 
A

Alexey Muranov

Thanks Thomas, this works!

Is this the easiest way??
Does not look too natural.

Alexey.
 
J

Joel VanderWerf

Hello, i have another question.

I have a strange situation which i have not resolved yet where for some
reason i have to wrap several classes in a module, otherwise the program
doesn't work as expected (by me of course).
Can anybody please tell me the following: is it possible to read class
definition from another file and wrap them in a module?

Here is an example:

I have several classes defined in a file models.rb:

class Person
...
end

class Address
...
end

I want to load or require this file in another file, say program.rb, so
that the classes were wrapped together in a module M, like

module M
load "models.rb"
end

p = M::person.new

but this doesn't work (M::person is an uninitialized constant, according
to an error message).

The basic trick is module_eval. Here's a library that does that for you
and has a few other features:

http://redshift.sourceforge.net/script

Your program.rb would be this:

require 'script'
M = Script.load "models.rb" # M is now a module
person = M::person.new
 
A

Alexey Muranov

Thanks Joel, i'll consider your library.

For me it was only important to make it work without modifying the
loaded file (i still do not understand why it doesn't work when not
wrapped in a module, but this is not so important).
So module_eval works for me.
Still it is somewhat unexpected (against the Least Surprise principle)
that calling "load" inside a module does not load the source inside that
module.

Alexey.
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top