Calling a method of a Ruby Singleton without the reference of 'instance'

前端 未结 5 2128
悲&欢浪女
悲&欢浪女 2021-01-21 03:37

I would like to call a method of a Singleton Object without the reference to its instance

SingletonKlass.my_method

instead of

S         


        
5条回答
  •  日久生厌
    2021-01-21 04:08

    Instead of SingletonKlass, I'll use the name UrlHelper as a more concrete example.

    require 'singleton'    
    
    class UrlHelperSingleton
      include Singleton
    
      def my_method
         ...
      end
    end
    
    UrlHelper = UrlHelperSingleton.instance
    
    # then elsewhere
    UrlHelper.my_method
    

提交回复
热议问题