Ruby class with static method calling a private method?

前端 未结 3 1230
春和景丽
春和景丽 2021-02-03 21:56

I have a class with a number of static methods. Each one has to call a common method, but I\'m trying not to expose this latter method. Making it private would only allow acce

相关标签:
3条回答
  • 2021-02-03 22:15

    You can define a private class method with private_class_method like this:

    class Foo
      def self.bar
        do_calc
      end
    
      def self.baz
        do_calc
      end
    
      def self.do_calc
        #...
      end
      private_class_method :do_calc
    end
    
    0 讨论(0)
  • 2021-02-03 22:22

    Or as of Ruby 2.1:

    class Foo
      def self.bar
        do_calc
      end
    
      private_class_method def self.do_calc
        #...
      end
    end
    
    0 讨论(0)
  • 2021-02-03 22:25

    First off, static is not really part of the Ruby jargon.

    Let's take a simple example:

    class Bar
      def self.foo
      end
    end
    

    It defines the method foo on an explicit object, self, which in that scope returns the containing class Bar. Yes, it can be defined a class method, but static does not really make sense in Ruby.

    Then private would not work, because defining a method on an explicit object (e.g. def self.foo) bypasses the access qualifiers and makes the method public.

    What you can do, is to use the class << self syntax to open the metaclass of the containing class, and define the methods there as instance methods:

    class Foo
      class << self
    
        def bar
          do_calc
        end
    
        def baz
          do_calc
        end
    
        private
    
        def do_calc
          puts "calculating..."
        end
      end
    end
    

    This will give you what you need:

    Foo.bar
    calculating...
    
    Foo.baz
    calculating...
    
    Foo.do_calc
    NoMethodError: private method `do_calc' called for Foo:Class
    
    0 讨论(0)
提交回复
热议问题