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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:43:04+00:00 2026-05-27T04:43:04+00:00

The following code works: class MyClass def method_a method_b end private def method_b puts

  • 0

The following code works:

class MyClass
  def method_a
    method_b
  end

  private
  def method_b
    puts "Hello!"
  end
end

m = MyClass.new
m.method_a

Changing the call to method_b to self.method_b however does not work:

def method_a
  self.method_b
end

I get a NoMethodError. I’m under the impression that self just resolves to the instance of the class when inside an instance method. Why does self.method_b cause problems?

Note: self.method_b works when private is changed to protected.

Note: if the above methods are changed to class methods then calling self.method_b from method_a doesn’t throw the NoMethodError.

  • 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-27T04:43:05+00:00Added an answer on May 27, 2026 at 4:43 am

    This is how private methods work in Ruby. They cannot be called with an explicit receiver (unless it is a setter method; see below).

    Read more in the section on Access Control from the Pickaxe.

    Private methods whose names end with an = may be invoked using self.method_name = ... as this is necessary to differentiate them from setting a local variable:

    class Foo
      def try
        set_foo(1)            # Invokes private method
        self.set_foo(2)       # ERROR: private method `set_foo' called for ...
        self.send(:set_foo,3) # Invokes private method
    
        bar = 1               # Sets local variable
        self.bar = 2          # Invokes private method
        self.send(:bar=,3)    # Invokes private method
      end
    
      private
        def set_foo(v)
          @foo = v
        end
        def bar=(v)
          @bar = v
        end
    end
    
    Foo.new.bar = 42          # ERROR: private method `bar=' called for ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: class MyClass def method foo = MyClass.all end end
The following code works: @Stateless @LocalBean public class MyClass { @PersistenceContext(name = MyPU) EntityManager
Please consider the following code: public class MyClass extends javax.swing.JFrame { private JTree jTree;
I have my code organized as following class MyClass { public: typedef void (MyClass::Ptr2func)();
I have the following code in my class : sub new { my $class
I have the following piece of HTML code: <div class=myclass id =class1> <ol class=list>
I have the following html: <a href=javascript:<%# 'js code' %> class=MyClass> <div class=HeaderRow style=vertical-align:middle;text-align:left;height:35px;width:998px;>
I'm a bit confused. I have the following code: public class MyClass { public
Today I encountered the following situation: (pseudo code) class MyClass { public void workOnArray(Object[]
DataAnnotations does not work with buddy class. The following code always validate true. Why

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.