def mylog(str) puts __FILE__,":"__LINENO__,":",str # Here how to get __FILE__ and __LINENO__ is my question. end
当我打电话给mylog:
mylog 'hello' # say I call this in my.rb line 10
我期待输出:
my.rb:10:hello
请帮助正确实现mylog功能.
def mylog(str) caller_line = caller.first.split(":")[1] puts "#{__FILE__} : #{caller_line} : #{str}" end
您可能想要知道调用mylog的文件…
def mylog(str) caller_infos = caller.first.split(":") puts "#{caller_infos[0]} : #{caller_infos[1]} : #{str}" end