Ruby-on-rails 写入 Rails 控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14961691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:25:51  来源:igfitidea点击:

Write to rails console

ruby-on-railsrubydebuggingdevelopment-environmentrails-console

提问by freemanoid

When I want to try or debug smthing I run rails consoleand 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 consolewithout exception raising (and obvious breaking code execution) like a simple logger.info "blah"?

当我想尝试或调试 smthing 时,我会在rails console那里运行并做一些事情。我可以通过使用raise "blablabla". 问:我怎么可以只写轨道控制台无一例外提升(和执行断码明显)是一个简单的logger.info "blah"

回答by Nobita

As other have said, you want to use either putsor p. Why? Is that magic?

正如其他人所说,您想使用putsp。为什么?那是魔法吗?

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,因此您可以在 IRB 中执行的所有操作都可以在 rails 控制台中执行。由于在 IRB 中打印,我们使用puts,我们使用相同的命令在 rails 控制台中打印。

You can actually take a look at the console codein the rails source code. See the require of irb? :)

实际上,你可以看看控制台代码中轨的源代码。看到irb的要求了吗?:)

回答by SeriousM

putsor pis a good start to do that.

puts或者p是一个好的开始。

p "asd"       # => "asd"
puts "asd"    # => asd

here is more information about that: http://www.ruby-doc.org/core-1.9.3/ARGF.html

这里有更多相关信息:http: //www.ruby-doc.org/core-1.9.3/ARGF.html

回答by NIA

In addition to already suggested pand puts— well, actually in most cases you do can write logger.info "blah"just as you suggested yourself. It works in console too, not only in server mode.

除了已经建议的pputs- 好吧,实际上在大多数情况下,您可以logger.info "blah"按照自己的建议进行编写。它也适用于控制台,不仅适用于服务器模式。

But if all you want is console debugging, putsand pare much shorter to write, anyway.

但是,如果您想要的只是控制台调试,puts并且p编写时间要短得多,无论如何。

回答by matiasmasca

I think you should use the Rails debug options:

我认为您应该使用 Rails 调试选项:

logger.debug "Person attributes hash: #{@person.attributes.inspect}"
logger.info "Processing the request..."
logger.fatal "Terminating application, raised unrecoverable error!!!"

https://guides.rubyonrails.org/debugging_rails_applications.html

https://guides.rubyonrails.org/debugging_rails_applications.html