What is the best way for me to determine a controller variable's value during execution?
在执行期间确定控制器变量值的最佳方法是什么?
For example, is there a way I can insert a break in the code, and cause the value of the variable to be output to the screen (or the log)?
例如,有没有办法在代码中插入一个中断,并将变量的值输出到屏幕(或日志)?
7 个解决方案
#1
11
Yes. The easiest way is to raise the value as a string. Like so: raise @foo.to_s
是。最简单的方法是将值提升为字符串。像这样:raise @ foo.to_s
Or, you can install the debugger (gem install ruby-debug
), and then start the development server with the --debugger
flag. Then, in your code, call the debugger
instruction.
或者,您可以安装调试器(gem install ruby-debug),然后使用--debugger标志启动开发服务器。然后,在您的代码中,调用调试器指令。
Inside the debugger prompt, you have many commands, including p
to print the value of a variable.
在调试器提示符内,您有许多命令,包括p来打印变量的值。
Update: here's a bit more about ruby-debug.
更新:这里有关于ruby-debug的更多信息。
#2
5
If you have a controller instance variable named @foo
, then in your controller you can simply do something like:
如果您有一个名为@foo的控制器实例变量,那么在您的控制器中,您可以执行以下操作:
logger.debug "@foo is: #{@foo}"
Additionally, you can output the value in your view template using:
此外,您可以使用以下方法在视图模板中输出值:
<%= debug @foo %>
#3
5
I prefer using the inspect method like so:
我更喜欢使用这样的检查方法:
raise @foo.inspect
It has more information than to_s, like the attribute values.
它具有比to_s更多的信息,如属性值。
#4
3
Summary from Jordi Bunster, John Topley, and Jaryl:
Jordi Bunster,John Topley和Jaryl的总结:
I. Quick and dirty way:
I.快捷方式:
raise @foo.inspect
in your controller. Or
在你的控制器中。要么
<% raise @foo.inspect %>
in your view.
在你看来。
II. Proper logging to you development.log
:
II。正确登录您的development.log:
logger.debug "@foo == #{@foo.inspect}"
III. Full-fledged debugging:
III。全面的调试:
Install the debugger (gem install ruby-debug
), and then start the development server with the --debugger
flag. Then, in your code, call the debugger
instruction.
安装调试器(gem install ruby-debug),然后使用--debugger标志启动开发服务器。然后,在您的代码中,调用调试器指令。
Inside the debugger prompt, you have many commands, including p
to print the value of a variable.
在调试器提示符内,您有许多命令,包括p来打印变量的值。
#5
0
Raising an exception is the fastest way if you just need to look at a value, but it's worth the time to learn how to use the debugger properly. It's rare that you would only need to just see the value of a variable, you are likely trying to find a bug in your code, and that's what a debugger is for.
如果您只需要查看值,则提出异常是最快的方法,但是值得花时间学习如何正确使用调试器。您很少需要只看到变量的值,您可能正试图在代码中找到错误,这就是调试器的用途。
Sending the info to the development log is slower than either of the other two options here so far if you learn how to use the debugger (who wants to read through log files). Use the logger for production, you are going to want to see what the value was when somebody calls you up and says everything is broken.
到目前为止,如果您学习如何使用调试器(谁想要读取日志文件),那么将信息发送到开发日志的速度要慢于其他两个选项。使用记录器进行生产,您将希望看到当有人打电话给您并说一切都坏了时的价值。
#6
0
Well, I usually prefer the standard error output
好吧,我通常更喜欢标准的错误输出
$stderr.print("whatever")
Its simple and does the job.
它简单而且干得好。
#7
0
- Add pry-moves to Gemfile:
gem 'pry-moves'
- Insert
binding.pry
where you want to stop - Type variable's name to see its value
添加pry-move到Gemfile:gem'prie-moves'
将binding.pry插入要停止的位置
输入变量的名称以查看其值
Then continue by typing c
, move to next line with n
or perform other debugging actions until you will resolve the issue.
然后继续输入c,使用n移动到下一行或执行其他调试操作,直到您将解决问题。
#1
11
Yes. The easiest way is to raise the value as a string. Like so: raise @foo.to_s
是。最简单的方法是将值提升为字符串。像这样:raise @ foo.to_s
Or, you can install the debugger (gem install ruby-debug
), and then start the development server with the --debugger
flag. Then, in your code, call the debugger
instruction.
或者,您可以安装调试器(gem install ruby-debug),然后使用--debugger标志启动开发服务器。然后,在您的代码中,调用调试器指令。
Inside the debugger prompt, you have many commands, including p
to print the value of a variable.
在调试器提示符内,您有许多命令,包括p来打印变量的值。
Update: here's a bit more about ruby-debug.
更新:这里有关于ruby-debug的更多信息。
#2
5
If you have a controller instance variable named @foo
, then in your controller you can simply do something like:
如果您有一个名为@foo的控制器实例变量,那么在您的控制器中,您可以执行以下操作:
logger.debug "@foo is: #{@foo}"
Additionally, you can output the value in your view template using:
此外,您可以使用以下方法在视图模板中输出值:
<%= debug @foo %>
#3
5
I prefer using the inspect method like so:
我更喜欢使用这样的检查方法:
raise @foo.inspect
It has more information than to_s, like the attribute values.
它具有比to_s更多的信息,如属性值。
#4
3
Summary from Jordi Bunster, John Topley, and Jaryl:
Jordi Bunster,John Topley和Jaryl的总结:
I. Quick and dirty way:
I.快捷方式:
raise @foo.inspect
in your controller. Or
在你的控制器中。要么
<% raise @foo.inspect %>
in your view.
在你看来。
II. Proper logging to you development.log
:
II。正确登录您的development.log:
logger.debug "@foo == #{@foo.inspect}"
III. Full-fledged debugging:
III。全面的调试:
Install the debugger (gem install ruby-debug
), and then start the development server with the --debugger
flag. Then, in your code, call the debugger
instruction.
安装调试器(gem install ruby-debug),然后使用--debugger标志启动开发服务器。然后,在您的代码中,调用调试器指令。
Inside the debugger prompt, you have many commands, including p
to print the value of a variable.
在调试器提示符内,您有许多命令,包括p来打印变量的值。
#5
0
Raising an exception is the fastest way if you just need to look at a value, but it's worth the time to learn how to use the debugger properly. It's rare that you would only need to just see the value of a variable, you are likely trying to find a bug in your code, and that's what a debugger is for.
如果您只需要查看值,则提出异常是最快的方法,但是值得花时间学习如何正确使用调试器。您很少需要只看到变量的值,您可能正试图在代码中找到错误,这就是调试器的用途。
Sending the info to the development log is slower than either of the other two options here so far if you learn how to use the debugger (who wants to read through log files). Use the logger for production, you are going to want to see what the value was when somebody calls you up and says everything is broken.
到目前为止,如果您学习如何使用调试器(谁想要读取日志文件),那么将信息发送到开发日志的速度要慢于其他两个选项。使用记录器进行生产,您将希望看到当有人打电话给您并说一切都坏了时的价值。
#6
0
Well, I usually prefer the standard error output
好吧,我通常更喜欢标准的错误输出
$stderr.print("whatever")
Its simple and does the job.
它简单而且干得好。
#7
0
- Add pry-moves to Gemfile:
gem 'pry-moves'
- Insert
binding.pry
where you want to stop - Type variable's name to see its value
添加pry-move到Gemfile:gem'prie-moves'
将binding.pry插入要停止的位置
输入变量的名称以查看其值
Then continue by typing c
, move to next line with n
or perform other debugging actions until you will resolve the issue.
然后继续输入c,使用n移动到下一行或执行其他调试操作,直到您将解决问题。