Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7090129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:00:29+00:00 2026-05-28T08:00:29+00:00

I have an external file: path_to_external_file.rb with some class definition: class A some_definitions end

  • 0

I have an external file: path_to_external_file.rb with some class definition:

class A
  some_definitions
end

And I want to load that within module B so that the class A defined above can be referred to as B::A. I tried:

class B
  load('path_to_external_file.rb')
end

but A is defined in the main environment, not in B:

A #=> A
B.constants # => []

How can I load external files within some class/module?

Edit
Should I read the external files as strings, and evaluate them within Class.new{...}, and include that class within B?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:00:30+00:00Added an answer on May 28, 2026 at 8:00 am

    You cannot. At least using load or require, the Ruby files will always be evaluated in a top context.

    You can work around that problem in two ways:

    • Define class B::A directly (but you are probably trying to avoid that)
    • Use eval(File.read("path_to_external_file.rb")) within your B class

    Edit: Maybe, this library is interesting for you: https://github.com/dreamcat4/script/blob/master/intro.txt

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python script that connects to an external host, fetches some data,
I want to load an external XML file in a unit test to test
I have some Ant tasks in my Eclipse JDT project that I want to
I have an external JAR signpost-core-1.2.1.1.jar (from http://code.google.com/p/oauth-signpost/ ), that I want to include
I have developed some reusable android component which is basically a class . This
I have a maven project that loads an xslt file and executes the transformation
SOLVED (Thanks Regexident) I have an app that passes the file path of a
We have a c file called dbookpy.c, which will provide a Python binding some
I have a Python script that needs to execute an external program, but for
i have a problem with my code that is supposed to write some data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.