Private module methods in Ruby

后端 未结 9 1083
深忆病人
深忆病人 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 06:56

    This method won't allow sharing data with the private methods unless you explicitly pass the data by method parameters.

    module Thing
      extend self
    
      def pub
        puts priv(123)
      end
    
      private
      
      def priv(value)
        puts "Private method with value #{value}"
      end
    end
    
    Thing.pub
    # "Private method with value 123"
    
    Thing.priv
    # NoMethodError (private method `priv' called for Thing:Module)
    

提交回复
热议问题