开发者

How to set just one method to private in Ruby?

开发者 https://www.devze.com 2023-03-07 18:07 出处:网络
I have a class with N methods. I want to set one of these methods to private. How can I do 开发者_如何学运维this? class Example

I have a class with N methods. I want to set one of these methods to private. How can I do 开发者_如何学运维this?


 class Example
    def methodA
    end

    def methodP
    end

    private :methodP
  end


I like this way:

class Example
    def public_method1
    end

    private def used_by_public_method1
    end

    def public_method2
    end
end

Another option (that I find more confusing):

class Example
    def public_method1
    end

    def public_method2
    end

    private

    def used_by_public_method1
    end

    # Don't accidentally put public methods down here.
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号