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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:26:25+00:00 2026-05-18T09:26:25+00:00

class Example private def example_test puts ‘Hello’ end end e = Example.new e.example_test This

  • 0
class Example
 private
 def example_test
  puts 'Hello'
 end
end

e = Example.new
e.example_test

This of course will not work, because we specified explicit receiver – instance of Example (e), and that is against a “private rule”.

But I cannot understand, why one cannot do in Ruby this:

class Foo
 def public_m
  self.private_m # <=
 end
 private
 def private_m
  puts 'Hello'
 end
end

Foo.new.public_m

The current object inside public_m method definition (i.e. self) is the instance of Foo. So why it is not allowed? To fix that I have to change self.private_m to just private_m. But why this differ, isn’t the self an instance of Foo inside public_m? And who is the receiver of bare-word private_m call? Isn’t that self – what actually you omit because, Ruby will do it for you (will call private_m on self)?

I hope I didn’t confuse it too much, I am still fresh to Ruby.


EDIT:
Thank you for all the answers. Putting them all together I was able (finally) to grok the obvious (and not so obvious for someone, who have never seen things like Ruby): that self itself can be
explicit and implicit receiver and that make the difference. So there are two rules, if you want to call a private method: self must be implicit receiver, and that self must be an instance of current class (Example in that case – and that takes place only when self if inside instance method definition, during this method execution). Please correct me if I am wrong.

class Example 

 # self as an explicit receiver (will throw an error)
 def explicit 
  self.some_private_method
 end

 # self as an implicit receiver (will be ok)
 def implicit
  some_private_method
 end

 private

 def some_private_method; end
end

Example.new.implicit

Message for anyone who could find this question during the google trails: this may be helpful – http://weblog.jamisbuck.org/2007/2/23/method-visibility-in-ruby

  • 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-18T09:26:25+00:00Added an answer on May 18, 2026 at 9:26 am

    Here’s the short and the long of it. What private means in Ruby is a method cannot be called with an explicit receivers, e.g. some_instance.private_method(value). So even though the implicit receiver is self, in your example you explicitly use self so the private methods are not accessible.

    Think of it this way, would you expect to be able to call a private method using a variable that you have assigned to an instance of a class? No. Self is a variable so it has to follow the same rules. However when you just call the method inside the instance then it works as expected because you aren’t explicitly declaring the receiver.

    Ruby being what it is you actually can call private methods using instance_eval:

    class Foo
      private
      def bar(value)
        puts "value = #{value}"
      end
    end
    
    f = Foo.new
    begin
      f.bar("This won't work")
    rescue Exception=>e
      puts "That didn't work: #{e}"
    end
    f.instance_eval{ bar("But this does") }
    

    Hope that’s a little more clear.

    — edit —

    I’m assuming you knew this will work:

    class Foo
     def public_m
      private_m # Removed self.
     end
     private
     def private_m
      puts 'Hello'
     end
    end
    
    Foo.new.public_m
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
take a look at this example code: public class Comment { private Comment() {
Suppose I've got the following program: namespace ReflectionTest { public class Example { private
As an example: public class Foo { private Foo() {} } public class Bar
here is my example code: Public Class Parent Private _TestProperty As String Private WithEvents
A little example TTest<T> = class private f : T; public function ToString :
Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private
What does it mean to call a class like this: class Example { public:
If I have a class as follows class Example_Class { private: int x; int
Is there a class/example application for a message-only window that is in C++ Win32

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.