On May 1, 1:55=A0pm, 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:
>
> =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