requiring the same file and capturing the constants

Discussion in 'Ruby' started by Raj Singh, May 13, 2008.

  1. Raj Singh

    Raj Singh Guest

    file1.txt
    -----------------
    class Person
    end
    module Speak
    end


    The task is to printout all the constants listed in the file. In this
    case there are Person and Speak.

    I need to handle two cases.
    1) The system loads the file for the first time. I guess in this case I
    can use ObjectSpace.each_object or ObjectSpace.some_method from API to
    get before and after picture. Subtract the before picture and I will get
    the constants.

    2) The second case is when file1.txt is already loaded into the
    application. So in this case I can't do before and after thing. How can
    I get constants in this case without before and after picture.

    Thanks
    --
    Posted via http://www.ruby-forum.com/.
    Raj Singh, May 13, 2008
    #1
    1. Advertising

  2. Raj Singh wrote:
    > file1.txt
    > -----------------
    > class Person
    > end
    > module Speak
    > end
    >
    >
    > The task is to printout all the constants listed in the file. In this
    > case there are Person and Speak.
    >
    > I need to handle two cases.
    > 1) The system loads the file for the first time. I guess in this case I
    > can use ObjectSpace.each_object or ObjectSpace.some_method from API to
    > get before and after picture. Subtract the before picture and I will get
    > the constants.
    >
    > 2) The second case is when file1.txt is already loaded into the
    > application. So in this case I can't do before and after thing. How can
    > I get constants in this case without before and after picture.
    >
    > Thanks


    If you have control over *how* the file is loaded, you can do this,
    using http://redshift.sourceforge.net/script:

    [~/tmp] cat file.rb
    class Person
    end
    module Speak
    end
    [~/tmp] cat main.rb
    require 'script'
    mod = Script.load 'file.rb'
    p mod.constants
    [~/tmp] ruby main.rb
    ["Person", "Speak"]

    The trick is really just reading the file, creating a module, and
    calling module_eval on that with the contents of the file.

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, May 13, 2008
    #2
    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. Eric
    Replies:
    2
    Views:
    1,457
    Tommy
    Feb 13, 2004
  2. Alexander Grigoriev
    Replies:
    0
    Views:
    386
    Alexander Grigoriev
    Sep 12, 2003
  3. Carlos
    Replies:
    3
    Views:
    608
  4. Jim Freeze

    __FILE__ for requiring file

    Jim Freeze, Oct 30, 2006, in forum: Ruby
    Replies:
    6
    Views:
    102
  5. Marc Heiler

    Requiring more than one file?

    Marc Heiler, Jun 10, 2007, in forum: Ruby
    Replies:
    12
    Views:
    175
    Rubén Medellín
    Aug 2, 2007
Loading...

Share This Page