Private module methods in Ruby

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

    What's about storing methods as lambdas within class variables/constants?

    module MyModule
      @@my_secret_method = lambda {
        # ...
      }
      # ...
    end
    

    For test:
    UPD: huge update of this code after 6 years shows cleaner way to declare private method d

    module A
      @@L = lambda{ "@@L" }
      def self.a ; @@L[] ; end
      def self.b ; a ; end
    
      class << self
        def c ; @@L[] ; end
        private
        def d ; @@L[] ; end
      end
      def self.e ; c ; end
      def self.f ; self.c ; end
      def self.g ; d ; end
      def self.h ; self.d ; end
    
      private
      def self.i ; @@L[] ; end
      class << self
        def j ; @@L[] ; end
      end
    
      public
      def self.k ; i ; end
      def self.l ; self.i ; end
      def self.m ; j ; end
      def self.n ; self.j ; end
    end
    
    for expr in %w{ A.a A.b A.c A.d A.e A.f A.g A.h A.i A.j A.k A.l A.m A.n }
      puts "#{expr} => #{begin ; eval expr ; rescue => e ; e ; end}"
    end
    

    Here we see that:

    A.a => @@L
    A.b => @@L
    A.c => @@L
    A.d => private method `d' called for A:Module
    A.e => @@L
    A.f => @@L
    A.g => @@L
    A.h => private method `d' called for A:Module
    A.i => @@L
    A.j => @@L
    A.k => @@L
    A.l => @@L
    A.m => @@L
    A.n => @@L
    

    1) @@L can not be accesses from outside but is accessible from almost everywhere
    2) class << self ; private ; def successfully makes the method d inaccessible from outside and from inside with self. but not without it -- this is weird
    3) private ; self. and private ; class << self do not make methods private -- they are accessible both with and without self.

提交回复
热议问题