简体   繁体   English

如何将IRB输入保存到.rb文件?

[英]How do you save IRB inputs to a .rb file?

Might sound like a newbie question (and it is since I am new to Ruby and IRB) but is there a way to save the commands you did in IRB to file? 可能听起来像一个新手问题(因为我是Ruby和IRB的新手)但有没有办法将您在IRB中执行的命令保存到文件中? I am playing with WATIR and would love to save all my inputs to file instead of copying and pasting each. 我正在玩WATIR并希望将所有输​​入保存到文件中,而不是复制和粘贴每个。

Thanks for the help! 谢谢您的帮助!

On my machine I can put this in my .irbrc file (located in your home directory): 在我的机器上,我可以将它放在我的.irbrc文件中(位于您的主目录中):

Kernel.at_exit {
  File.open("irb.log", "w") do |f|
    f << Readline::HISTORY.to_a.join("\n")
  end
}

It creates a file irb.log that contains your readline history. 它会创建一个包含您的readline历史记录的文件irb.log Irb uses readline for command input. Irb使用readline进行命令输入。 It might be configured not to use readline for some people, I don't know. 它可能被配置为不对某些人使用readline,我不知道。 And maybe the history will be truncated at some point, or maybe it'll be modified by certain commands you do in your irb session... but try it out and see if it works. 也许历史将在某些时候被截断,或者它可能会被您在irb会话中执行的某些命令修改...但是试试看它是否有效。

If you want the irb prompt and the result of each command to be included in the log, then just use tee to record the output of irb: 如果你想要irb提示符和每个命令的结果都包含在日志中,那么只需使用tee来记录irb的输出:

$ irb | tee irb.log

I found this question when looking to do the same thing. 我在寻找同样的事情时发现了这个问题。 I ended up switching from IRB to Pry; 我最终从IRB转到了Pry; it is a separate REPL project for Ruby that has a whole host of advanced features not supported in IRB. 它是Ruby的一个单独的REPL项目,它具有IRB不支持的许多高级功能。

Well worth a look. 非常值得一看。

Pry

看看watir-console

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM