Understanding private methods in Ruby
Asked Answered
A

8

52
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

Abaxial answered 27/11, 2010 at 18:34 Comment(1)
Here is a very similar question.Evidentiary
C
55

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
Cantal answered 27/11, 2010 at 19:1 Comment(2)
Well, I think I understand it now (or - I am much closer to understanding it :)Abaxial
Since you quote Section 512, I hope you are aware that under the same subsection that allows notices of infringement, such a notice must be delivered to the designated agent for the business in question. AFAIK, editing an answer on Stack Overflow is not a recourse provided by the law. Stack Overflow includes a helpful guide and full contact info on its Legal page. (CYA note: This comment is off-the-cuff, for informational value only, and should not be construed as legal advice.)Esteban
M
17

The definition of private in Ruby is "can only be called without an explicit receiver". And that's why you can only call private methods without an explicit receiver. There is no other explanation.

Note that there actually is an exception to the rule: because of the ambiguity between local variables and method calls, the following will always be resolved to be an assignment to a local variable:

foo = :bar

So, what do you do if you want to call a writer called foo=? Well, you have to add an explicit receiver, because without the receiver Ruby simply won't know that you want to call the method foo= instead of assigning to the local variable foo:

self.foo = :bar

But what do you do if you want to call a private writer called foo=? You can't write self.foo = because foo= is private and thus cannot be called with an explicit receiver. Well, actually for this specific case (and this case alone), you can actually use an explicit receiver of self to call a private writer.

Madonnamadora answered 27/11, 2010 at 21:27 Comment(6)
Erase the first two sentences and you have a great answer. You should have lead with, "The definition of private in Ruby is "can only be called without an explicit receiver". And that's why you can only call private methods without an explicit receiver. There is no other explanation."Cantal
Nice answer. If you're bored some time think this one through: self.foo ||= barTurfman
Thank you for mentioning writers exception. +1 even if I don't feel comfortable with answer that begin with "that's the way it is..". Regards.Abaxial
I tried googling ruby forum to see if there was an explanation on the "why", preferably from The Matz. I couldn't find one. Maybe someone with better google-fu could find it.Bromic
As you mentioned, the first two sentences didn't really help answering the question. I just removed them, so if someone wonders what the comments are about: It's not there anymore.Axial
OK at this point, Ruby is just being dumb. Given: (1) Well, actually for this specific case (and this case alone), you can actually use an explicit receiver of self to call a private writer, (2) Self is a variable so it has [?, not really only by definition] to follow the same rules. The definition just be changed. (3) If self.foo ||= bar works then it's also able to access a private reader.Matsuyama
T
14

It's weird, but many things about Ruby's visibility modifiers are weird. Even if self is the implicit receiver, actually spelling it out makes it explicit in the eyes of the Ruby runtime. When it says that private methods cannot be called with an explicit receiver, that is what it means, even self counts.

Tighe answered 27/11, 2010 at 19:3 Comment(0)
R
3

IIRC, private methods allow only implicit receiver (which is always self, of course).

Retinol answered 27/11, 2010 at 18:46 Comment(0)
E
1

Sorry for my prevoius answer. I just don't understand your question.

I changed your code like this:

class Foo
 def public_m
  private_m # <=
 end

 def Foo.static_m
   puts "static"
 end

 def self.static2_m
   puts "static 2"
 end

 private 
 def private_m
  puts 'Hello'
 end
end

Foo.new.public_m
Foo.static_m
Foo.static2_m

Here is a call of instance method:

 def public_m
  private_m # <=
 end

Here are a call of class methods:

 def Foo.static_m
   puts "static"
 end

 def self.static2_m
   puts "static 2"
 end

Foo.static_m
Foo.static2_m
Evidentiary answered 27/11, 2010 at 19:10 Comment(2)
I think you are missing the point. From what I understand of his question he doesn't understand why using self.private_m doesn't work. I'm assuming he's used to languages that let you use self or this to call instance methods or use instance variables. I think he's confused about why using Self is hosing up the works, not how private and public accessors work.Cantal
rather than create multiple answers, edit or extend your previous answer. That helps us keep things in context. Thanks.Feisty
O
1

Adding some enhancements to User Gates solution. Calling a private method to class method or an instance method is pretty much possible. Here is the Code Snippets. But not recommended.

Class Method

class Example
  def public_m
    Example.new.send(:private_m)
  end

  private
  def private_m
    puts 'Hello'
  end
end

e = Example.new.public_m

Instance Method

class Example
  def self.public_m
    Example.new.send(:private_m)
  end

  private
  def private_m
    puts 'Hello'
  end
end

e = Example.public_m
Oballa answered 9/2, 2018 at 9:20 Comment(0)
M
0

Does not exactly answer the Question, but you can call private methods this way

class Example
 private
 def example_test
  puts 'Hello'
 end
end

e = Example.new
e.send(:example_test)
Magna answered 27/2, 2017 at 12:47 Comment(0)
L
0

Just in case someone stumbles this now. From Ruby 2.7, calling a private method with a literal self as the receiver is now allowed.

We can verify this as well by running the original ruby snippet against versions 2.6.9 and 3.1.

(ins)tmp->cat sample.rb 
class Foo
 def public_m
  self.private_m # <=
 end
 private
 def private_m
  puts 'Hello'
 end
end

Foo.new.public_m


# See no exception is raise with 3.1 version
(ins)tmp->ruby -v
ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x86_64-darwin20]
(ins)tmp->ruby sample.rb 
Hello

Now if we try to run the same script with version 2.6.9, we see that exception is raised.

(ins)tmp->ruby -v
ruby 2.6.9p207 (2021-11-24 revision 67954) [x86_64-darwin20]
(ins)tmp->
(ins)tmp->ruby sample.rb 
sample.rb:3:in `public_m': private method `private_m' called for #<Foo:0x00007ff95289f870> (NoMethodError)
Did you mean?  private_methods
        from sample.rb:11:in `<main>'
Lindsaylindsey answered 21/1, 2023 at 23:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.