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 7060145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:19:03+00:00 2026-05-28T04:19:03+00:00

This is best explained with an example: file1.rb: def foo puts 123 end file2.rb:

  • 0

This is best explained with an example:

file1.rb:

def foo
  puts 123
end

file2.rb:

class A
  require 'file1'
end
A.new.foo

will give an error “‘: private method ‘foo’ called”.

I can get around this by doing A.new.send("foo") but is there a way to make the imported methods public?

Edit: To clarify, I am not confusing include and require. Also, the reason why I cannot use normal inclusion (as many have rightly pointed out) is that this is part of a meta-programming setup. I need to allow the user to add functionality at run-time; eg he can say “run-this-app –include file1.rb” and the app will behave differently based on the code he wrote in file1.rb. Sorry should have explained clearer.

Edit: After reading Jorg’s answer I realized my code does not behave exactly as intended, and he answers my (misguided) question perfectly. I am trying to do something more akin to str=(entire file1.rb as string); A.class_exec(str).

  • 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-28T04:19:03+00:00Added an answer on May 28, 2026 at 4:19 am

    Global procedures in Ruby aren’t really global procedures. They are methods, like everything else. In particular, when you define what looks like a global procedure, you are actually defining a private instance method of Object. Since every piece of code in Ruby is evaluated in the context of an object, this allows you to use those methods as if they were global procedures, since self is the default receiver, and self is an object whose class inherits from Object.

    So, this:

    # file1.rb
    
    def foo
      puts 123
    end
    

    is actually equivalent to

    # file1.rb
    
    class Object
      private
    
      def foo
        puts 123
      end
    end
    

    Now you have a “global procedure” called foo, which you can call just like this:

    foo
    

    The reason why you can call it like this, is that this call is actually equivalent to

    self.foo
    

    and self is an object that includes Object in its ancestry chain, thus it inherits the private foo method.

    [Note: to be precise, private methods cannot be called with an explicit receiver, even if that explicit receiver is self. So, to be really pedantic, it is actually equivalent to self.send(:foo) and not self.foo.]

    The A.new.foo in your file2.rb is a red herring: you could just as well try Object.new.foo or [].foo or 42.foo and get the same result.

    By the way: puts and require are themselves examples of such “global procedures”, which are actually private methods on Object (or more precisely, they are private methods on Kernel which is mixed into Object).

    On a sidenote: it is really bad style to put calls to require inside a class definition, because it makes it look like the required code is somehow scoped or namespaced inside the class, which is of course false. require simply runs the code in the file, nothing more.

    So, while

    # file2.rb
    
    class A
      require 'file1.rb'
    end
    

    is perfectly valid code, it is also very confusing. It is much better to use the following, semantically equivalent, code:

    # file2.rb
    
    require 'file1.rb'
    
    class A
    end
    

    That way it is perfectly clear to the reader of the code that file1.rb is in no way scoped or namespaced inside A.

    Also, it is generally preferred to leave off the file extension, i.e. to use require 'file1' instead of require 'file1.rb'. This allows you to replace the Ruby file with, for example, native code (for MRI, YARV, Rubinius, MacRuby or JRuby), JVM byte code in a .jar or .class file (for JRuby), CIL byte code in .dll file (for IronRuby) and so on, without having to change any of your require calls.

    One last comment: the idiomatic way to circumvent access protection is to use send, not instance_eval, i.e. use A.new.send(:foo) instead of A.new.instance_eval {foo}.

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

Sidebar

Related Questions

Best explained with code I think, this is just a simple example: public class
the best way to explain is with example so: this is the model public
I need to explain this by example: Is there a best practice or preference
I'm going to try this new cloud technology. So I think the best place
Best to be explained at an example: I am on branch 0.58 of repository
Difficult to try and phrase this question, but I will try my best. Basically,
My problem is best explained with examples: This works: $(startContainer).parents().each(function(index, parentNode) { if (parentNode.isSameNode(commonContainer))
I'm not sure how to best explain this, so this may be a bit
I want to explain this as best as I can. I have a Webgrid
I'll try my best to explain how I'm trying to set up this system.

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.