Understanding private methods in Ruby

前端 未结 7 1188
终归单人心
终归单人心 2020-11-29 00:01
class Example
 private
 def example_test
  puts \'Hello\'
 end
end

e = Example.new
e.example_test

This of course will not work, because we specifi

相关标签:
7条回答
  • 2020-11-29 00:42

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

    0 讨论(0)
  • 2020-11-29 00:43

    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)
    
    0 讨论(0)
  • 2020-11-29 00:45

    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.

    0 讨论(0)
  • 2020-11-29 00:57

    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
    
    0 讨论(0)
  • 2020-11-29 01:03

    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.

    0 讨论(0)
  • 2020-11-29 01:07

    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 讨论(0)
提交回复
热议问题