写到rails控制台 [英] Write to rails console
问题描述
当我想尝试或调试smthing时,我运行 rails console
并在那里做一些事情。我可以使用 raiseblablabla
提高异常来打印代码中的一些文本或变量。
问题:如何只需简单地向 rails console 写入,而不会像一个简单的 logger.info blah
?
When I want to try or debug smthing I run rails console
and do some stuff there. I can print some text or variables from code by raising exception with raise "blablabla"
.
Question: How I can just write to rails console without exception raising (and obvious breaking code execution) like a simple logger.info "blah"
?
推荐答案
正如其他人所说,你要使用 puts
或 p
。为什么?这是魔法吗?
As other have said, you want to use either puts
or p
. Why? Is that magic?
其实不是。导轨控制台在引擎盖下有一个 IRB ,所以你可以在IRB中做任何事情能够在导轨控制台中做。因为在IRB中打印,我们使用 puts
,我们使用相同的命令在rails控制台中打印。
Actually not. A rails console is, under the hood, an IRB, so all you can do in IRB you will be able to do in a rails console. Since for printing in an IRB we use puts
, we use the same command for printing in a rails console.
您可以在rails源代码中查看控制台代码 。看到irb的要求? :)
You can actually take a look at the console code in the rails source code. See the require of irb? :)
这篇关于写到rails控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!