AndrewShmig
AndrewShmig

Reputation: 4923

How to set just one method to private in Ruby?

I have a class with N methods. I want to set one of these methods to private. How can I do this?

Upvotes: 5

Views: 775

Answers (2)

MarredCheese
MarredCheese

Reputation: 20791

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

Upvotes: 0

Adrian Serafin
Adrian Serafin

Reputation: 7715

 class Example
    def methodA
    end

    def methodP
    end

    private :methodP
  end

Upvotes: 14

Related Questions