首页 > 解决方案 > 将简单的 Ruby 脚本及其输出保存在一个文件中

问题描述

我想将简单的 Ruby 脚本及其输出保存在一个文件中。我知道 jupyter-notebook 是一个很好的工具。Ruby 脚本可以通过使用 iruby 来使用 jupyter 执行。但我不想使用 iruby。因为 iruby 不稳定,而 jupyter 对我来说是过度设计的。

所以我正在寻找一种简单的方法。

你好.rb

puts "hello world"
2 + 3

执行一些不错的命令

irbliketool hello.rb > output.txt

output.txt 看起来像这样...

irb > puts "hello world"
hello world
=> nil

irb > 2+3
=> 5

有好的想法吗?

标签: rubyjupyter-notebookirbpry

解决方案


您可以将文件传递给irb.

irb hello.rb > output.txt

输出不完全相同,但您也可以这样做:

cat hello.rb | irb > out.txt

推荐阅读