Wrap external source file in a module

Discussion in 'Ruby' started by Alexey Muranov, May 1, 2011.

  1. 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.

    --
    Posted via http://www.ruby-forum.com/.
     
    Alexey Muranov, May 1, 2011
    #1
    1. Advertising

  2. Alexey Muranov

    Intransition Guest

    On May 1, 1:55=A0pm, Alexey Muranov <-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
     
    Intransition, May 1, 2011
    #2
    1. Advertising

  3. Thanks Thomas, this works!

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

    Alexey.

    --
    Posted via http://www.ruby-forum.com/.
     
    Alexey Muranov, May 1, 2011
    #3
  4. 2011/5/1 Alexey Muranov <-toulouse.fr>:
    > Thanks Thomas, this works!
    >
    > Is this the easiest way??
    > Does not look too natural.


    IMHO not natural is what you are trying to achieve ;)

    --=20
    I=C3=B1aki Baz Castillo
    <>
     
    Iñaki Baz Castillo, May 1, 2011
    #4
  5. 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).


    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
     
    Joel VanderWerf, May 1, 2011
    #5
  6. 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.

    --
    Posted via http://www.ruby-forum.com/.
     
    Alexey Muranov, May 1, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aaron Fude

    To wrap or not to wrap?

    Aaron Fude, May 8, 2008, in forum: Java
    Replies:
    12
    Views:
    717
    Chronic Philharmonic
    May 10, 2008
  2. Daniel Berger

    base64.rb - wrap in a module?

    Daniel Berger, Jul 31, 2003, in forum: Ruby
    Replies:
    0
    Views:
    97
    Daniel Berger
    Jul 31, 2003
  3. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 22, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    254
    Art Werschulz
    Sep 22, 2003
  4. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 24, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    257
    Anno Siegel
    Sep 25, 2003
  5. Mellow Crow
    Replies:
    6
    Views:
    425
    Richard Cornford
    Nov 4, 2005
Loading...

Share This Page