如何从终端运行 Ruby 代码?

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

How to run Ruby code from terminal?

rubyconsoleterminal

提问by Andresh Podzimovsky

I need to run a few lines of Ruby code from terminal, but I can't find the needed parameter for it.

我需要从终端运行几行 Ruby 代码,但找不到所需的参数。

Can you explain how to do this?

你能解释一下如何做到这一点吗?

回答by theglauber

If Ruby is installed, then

如果安装了 Ruby,那么

ruby yourfile.rb

where yourfile.rbis the file containing the ruby code.

yourfile.rb包含 ruby​​ 代码的文件在哪里。

Or

或者

irb

to start the interactive Ruby environment, where you can type lines of code and see the results immediately.

启动交互式 Ruby 环境,您可以在其中键入代码行并立即查看结果。

回答by LanguagesNamedAfterCofee

You can run ruby commands in one line with the -eflag:

您可以在一行中运行带有-e标志的ruby 命令:

ruby -e "puts 'hi'"

Check the man pagefor more information.

查看手册页以获取更多信息。