有没有办法在Rails控制台内的输出上实现类似grep的功能

sub*_*iet 9 ruby console grep ruby-on-rails

在shell中,我可以做到

$ cat name_of_file_with_a_lot_of_text | grep "What I am looking for"
Run Code Online (Sandbox Code Playgroud)

在Rails控制台内部,我可以实现类似的功能,例如当我运行命令并且输出很大时,尤其是数据库查询.

我知道将它输出为YAML,但这不是我想要的.

谢谢.

Mar*_*her 21

是的你可以.该方法称为gr ...等待它... ep.Ruby的grep作品上String,Array和许多其他内置对象.例如,要获取数字的所有to_xxx方法,只需执行以下操作:

 1.methods.grep(/to_/)
Run Code Online (Sandbox Code Playgroud)

  • 我只是大笑起来......“gr..wait it..ep”......就像一个幽默;) (2认同)