Where to place private methods in Ruby?

前端 未结 10 1022
故里飘歌
故里飘歌 2021-01-30 00:50

Most of the blogs or tutorials or books have private methods at the bottom of any class/module. Is this the best practice?

I find having private methods as and when nece

10条回答
  •  不知归路
    2021-01-30 01:15

    As others have already pointed out the convention is to put private methods at the bottom, under one private class. However, you should probably also know that many programers use a double indented (4 spaces instead of 2) method for this. The reason is that often times you won't see "private" in your text editor and assume they could be public. See below for an illustration:

    class FooBar
    
      def some_public_method
      end
    
      def another_public_method
      end
    
    private
    
        def some_private_method
        end
    
        def another_private method
        end
    
    end
    

    This method should prevent you from having to scroll up and down and will make other programmers more comfortable in your code.

提交回复
热议问题