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).
module M
module\_eval File\.read\(File\.dirname\(\_\_FILE\_\_\) \+ '/models\.rb'\)
end
p = M::Person\.new
···
On May 1, 1:55 pm, Alexey Muranov <mura...@math.univ-toulouse.fr> wrote:
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).
require 'script'
M = Script.load "models.rb" # M is now a module
person = M::Person.new
···
On 05/01/2011 10:55 AM, Alexey Muranov wrote:
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).
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.