在bigbold上看到Get the currently running method name in Ruby 代码片段:
module Kernel
 private
    
def this_method_name
      caller[0] 
=~ /`([^']*)'/ and $1
    end
end


class Foo
 
def test_method
   this_method_name
 end
end

puts Foo.new.test_method    
# => test_method
对其caller[0] =~ /`([^']*)'/ and $1这种语法结构甚是不解,后来又发现这么写也行caller[0][/`([^']*)'/, 1],于是查了下参考手册中

相关文章:

  • 2021-07-10
  • 2021-09-28
  • 2022-12-23
  • 2022-01-28
  • 2021-05-19
  • 2022-12-23
  • 2021-08-12
  • 2022-12-23
猜你喜欢
  • 2021-12-04
  • 2022-12-23
  • 2022-02-26
  • 2021-08-06
  • 2021-10-17
  • 2022-12-23
相关资源
相似解决方案