代码之家  ›  专栏  ›  技术社区  ›  Joe Zack SW4

如何在Ruby中动态确定方法名

  •  2
  • Joe Zack SW4  · 技术社区  · 17 年前

    例如:

    def example_method
      puts method_name
    end
    
    #=> "example_method"
    
    1 回复  |  直到 17 年前
        1
  •  3
  •   Community Mohan Dere    9 年前

    试试这个方法,从 here

    module Kernel
     private
      def current_method_name
        caller[0] =~ /`([^']*)'/ and $1
      end
    end
    
    
    class Foo
     def test_method
       current_method_name   # "test_method"
     end
    end
    

    here 本强调。

    推荐文章