Where to place private methods in Ruby?

前端 未结 10 1023
故里飘歌
故里飘歌 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:18

    There's also the option to prepend private to the method definition since Ruby 2.1.

    class Example
    
     def xmethod
     end
    
     private def ymethod
     end
    
     private def zmethod 
     end
    
    end
    

    Looking at the definition, you instantly know if a method is private, no matter where in the file it's defined. It's a bit more typing (if you don't autocomplete) and not all your defs will be nicely aligned.

提交回复
热议问题