有没有办法在 Rails 控制台内的输出上实现类似 grep 的功能
在 shell 中,我可以在 Rails Console 内执行
$ cat name_of_file_with_a_lot_of_text | grep "What I am looking for"
,我可以实现类似的功能吗,比如当我运行命令并且输出很大时,尤其是数据库查询。
我知道将其输出为 YAML,但这不是我想要的。
谢谢。
In shell, I can do
$ cat name_of_file_with_a_lot_of_text | grep "What I am looking for"
Inside the Rails Console, can I achieve something similar, say when I run a command and the output is huge, especially say a DB query.
I am aware of outputting it as YAML but that Is not what I am looking for.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你可以。该方法称为gr...wait it...ep。 Ruby 的
grep
适用于String
、Array
和许多其他内置对象。例如,要获取某个数字的所有to_xxx
方法,只需执行以下操作:Yes, you can. The method is called gr... wait for it ...ep. Ruby's
grep
works onString
,Array
and many other built-in objects. For example to get allto_xxx
methods of a number, just do:我也有同样的问题,并且对 ream88 的有点尖刻的回答不太满意,所以我决定尝试一下。
我的解决方案的明显缺点是输出丢失。我不知道如何在不调用
yield
两次的情况下解决这个问题。编辑我已将答案更改为仅调用
fork
一次,这使我可以保留块的输出并在最后返回它。赢。编辑2您现在可以在这个gem中获得所有这些功能(以及更多!)https: //github.com/FutureAdvisor/console_util
I had the same question and wasn't very satisfied with the somewhat snarky response from ream88, so I decided to take a crack at it.
The obvious drawback of my solution is that the output is lost. I'm not sure how to get around that without calling
yield
twice.EDIT I've changed my answer to only call
fork
once, which allows me to keep the output of the block and return it at the end. Win.EDIT 2 You can get all of this functionality (and more!) in this gem now https://github.com/FutureAdvisor/console_util