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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:31:55+00:00 2026-06-07T18:31:55+00:00

Ruby is pretty consistent about classes and objects. However, when it comes to top-level

  • 0

Ruby is pretty consistent about classes and objects. However, when it comes to top-level method declaration, that rule somehow breaks down. For example,

$ puts self # => main
$ puts self.class # => Object

However, the methods declared in the context of this main object are somehow available as private methods of class Object.

Is there any logical explanation for this? I understand that this is very convenient as these methods appear as “stand-alone functions”, but taking it just as “it just is so” breaks the otherwise consistent rules, as normally you have to define a method in the context of a class for it to be a method of that class. However, the main object is not class Object, nor is it a class at all.

Re-framing my question:
In what scope does the method gets declared inside ruby’s REPL? {class Object/ object main or module Kernel}

Regarding private nature of methods declared in REPL, plz see the following example:

def my_method # "a method declared in REPL"
  puts "method called"
end

# calling my_method in REPL aka top-level scope
my_method # => "method called"
[].my_method # => private method `my_method' called for []:Array (NoMethodError)
Array.my_method # => private method `my_method' called for Array:Class (NoMethodError)

Q: If my_method is defined as a private method under class Object, then why is self set to main instead of class Object. If someone says that its not under private scope, then why am I getting above mentioned error?

  • 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-06-07T18:31:56+00:00Added an answer on June 7, 2026 at 6:31 pm

    Although I’m not sure I fully understand the question, I’ll try to answer:

    When you’re working inside irb you’re really working in a context like this:

    class Object
      # you're here
    end
    

    So, if you declare a method inside the REPL you’re actually declaring it inside main‘s class:

    ruby-1.9.3-rc1 :001 > def foo; end
     => nil 
    ruby-1.9.3-rc1 :002 > self.class.public_methods.include?(:foo)
     => true 
    

    I’m not sure where you get the idea that the methods defined there are private to the class (but again, I might be misunderstanding the question).

    The class you’re inside in irb includes the Kernel module so that’s why you have access to its functions (which, since you’re inside the class, appear as stand-alone functions even though they’re regular methods):

    ruby-1.9.3-rc1 :003 > self.class.included_modules
     => [Kernel] 
    

    This would make it consistent with the typical Ruby behavior and indeed, main is just self inside the Object class.

    If you call self you’ll receive main because that’s the way it’s instructed to do it through inspect. We can remove the #to_s method from self and see the real value:

    ruby-1.9.3-p0 :001 > self
     => main 
    ruby-1.9.3-p0 :002 > class << self; remove_method :to_s; end
     => #<Class:#<Object:0x007fc08387af00>> 
    ruby-1.9.3-p0 :003 > self
     => #<Object:0x007fc08387af00 @prompt={:PROMPT_I=>"ruby-1.9.3-p0 :%03n > ", :PROMPT_S=>"ruby-1.9.3-p0 :%03n%l> ", :PROMPT_C=>"ruby-1.9.3-p0 :%03n > ", :PROMPT_N=>"ruby-1.9.3-p0 :%03n?> ", :RETURN=>" => %s \n", :AUTO_INDENT=>true}> 
    

    Edit: Before moving forward I think you should clarify which Ruby version are you using, see the sample below for 1.9.3:

    ruby-1.9.3-p0 :001 > def my_method
    ruby-1.9.3-p0 :002?>     puts "method called"
    ruby-1.9.3-p0 :003?> end
     => nil 
    ruby-1.9.3-p0 :004 > 
    ruby-1.9.3-p0 :005 >   [].my_method
    method called
     => nil 
    ruby-1.9.3-p0 :006 > Array.my_method 
    method called
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a pretty way to make a series of method calls in ruby
I am pretty sure that Ruby has these (equivalents for __call, __get and __set
I'm pretty sure ruby has an idiom for that. I just have too many
I've got a pretty simple Ruby on Rails app that has a text box
Undefining a method in Ruby is pretty simple, I can just use undef METHOD_NAME
Dependency Injection frameworks in Ruby have been pretty much declared unnecessary. Jamis Buck wrote
I'm pretty new to Ruby. I've tried looking over the online documentation, but I
I'm pretty new to Ruby on Rails. I changed this thread, because i recognized
This is pretty simple: how do I strip a ruby string of a special
Helo, I am pretty new to Ruby (using 1.8.6) and need to know whether

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.