Ruby-on-rails 停止 Rails 控制台在循环结束时打印出对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13284277/
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:00:15  来源:igfitidea点击:

Stop rails console from printing out the object at the end of a loop

ruby-on-railsrubyconsoleirb

提问by dsp_099

If I, say, loop through all the instances of a given model and output something from each, at the end, irb will still print the entire object.

例如,如果我遍历给定模型的所有实例并从每个实例中输出一些内容,那么最后 irb 仍将打印整个对象。

If the object ends up taking hundreds of lines, it'll be a long way up before I see what I was actually looking for. Is there a way to disable this in the rails console?

如果对象最终占用了数百行,那么在我看到我实际寻找的内容之前还有很长的路要走。有没有办法在 rails 控制台中禁用它?

回答by aef

If you don't want to disable the echo in general you could also call multiple expressions in one command line. Only the last expression's output will be displayed.

如果您不想在一般情况下禁用回声,您还可以在一个命令行中调用多个表达式。只会显示最后一个表达式的输出。

big_result(input); 0

回答by lulalala

Call conf.echo = falseand it will not print the return value. This works for any irb session, not just Rails console.

调用conf.echo = false它不会打印返回值。这适用于任何 irb 会话,而不仅仅是 Rails 控制台。

In case you want to make it permanent, add it to your irb config.

如果您想让它成为永久性的,请将其添加到您的 irb 配置中。

echo 'IRB.conf[:ECHO] = false' >> $HOME/.irbrc

回答by RajaRaviVarma

To temporarily stop the console from printing the return values you can issue a nilstatement at the end of your loop or function, but before pressing the return.

要暂时停止控制台打印返回值,您可以nil在循环或函数的末尾,但在按下 return 之前发出一条语句。

record.each do |r|
  puts r.properties
end; nil

Or it can be a number too, if you want to reduce typing. But it can be confusing in scenarios, which I can't think of.

或者它也可以是一个数字,如果你想减少打字。但它在场景中可能会令人困惑,这是我想不到的。

record.each do |r|
  puts r.properties
end; 0