Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 相当于PERL-Carp->;红宝石的咯咯声($msg)_Ruby_Perl_Stack Trace_Perl Module - Fatal编程技术网

Ruby 相当于PERL-Carp->;红宝石的咯咯声($msg)

Ruby 相当于PERL-Carp->;红宝石的咯咯声($msg),ruby,perl,stack-trace,perl-module,Ruby,Perl,Stack Trace,Perl Module,Perl有一个名为Carp的模块,可以从中打印消息(w.o明确地引发异常),打印消息和完整的堆栈跟踪。 即 将产生: foo called from file bar, line 2 你知道如何在Ruby中获得类似的东西吗?你可以使用内核#调用者_位置来实现这个() 输出: 1 called from file line_of_caller.rb, line 6 hello called from file line_of_caller.rb, line 7 loc这里是Thread::Ba

Perl有一个名为Carp的模块,可以从中打印消息(w.o明确地引发异常),打印消息和完整的堆栈跟踪。 即

将产生:

foo called from file bar, line 2
你知道如何在Ruby中获得类似的东西吗?

你可以使用
内核#调用者_位置
来实现这个()

输出:

1 called from file line_of_caller.rb, line 6
hello called from file line_of_caller.rb, line 7
loc
这里是Thread::Backtrace::Location的一个实例,因此您也可以从中获得更多信息;退房

def cluck(val)
  loc = caller_locations.last
  puts "#{val} called from file #{loc.path}, line #{loc.lineno}"
end

cluck 1
cluck "hello"
1 called from file line_of_caller.rb, line 6
hello called from file line_of_caller.rb, line 7