Private module methods in Ruby

后端 未结 9 1114
深忆病人
深忆病人 2020-12-04 06:21

I have a two part question

Best-Practice

  • I have an algorithm that performs some operation on a data structure using the public interfa
9条回答
  •  半阙折子戏
    2020-12-04 07:06

    A nice way is like this

    module MyModule
      class << self
        def public_method
          # you may call the private method here
          tmp = private_method
          :public
        end
    
        private def private_method
          :private
        end
      end
    end
    
    # calling from outside the module
    puts MyModule::public_method
    

提交回复
热议问题