Private module methods in Ruby

后端 未结 9 1122
深忆病人
深忆病人 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:14

    I think the best way (and mostly how existing libs are written) to do this is by creating a class within the module that deals with all the logic, and the module just provides a convenient method, e.g.

    module GTranslate
      class Translator
        def perform( text ); translate( text ); end
    
        private
    
        def translate( text )
          # do some private stuff here
        end
      end
    
      def self.translate( text )
        t = Translator.new
        t.perform( text )
      end
    end
    

提交回复
热议问题