如何从 Terminal.app 运行一段 ruby 代码?
我专门使用 OS X Terminal.app 进行命令行操作,但这个问题也可能适用于其他命令行工具。
假设我想从命令行运行这段 ruby 代码:
Cats.each do |cat|
cat.name = 'Mommy'
cat.kittens each do |kitten|
kitten.color = "Brown"
end
end
现在,如果我复制/粘贴,它就会被破坏并且不会执行。
I'm specifically using OS X Terminal.app for command line stuff, but this question may also apply to other command line tools.
Say I want to run this block of ruby code from the command line:
Cats.each do |cat|
cat.name = 'Mommy'
cat.kittens each do |kitten|
kitten.color = "Brown"
end
end
Right now if I copy/paste that it just gets broken up and doesn't execute.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请注意,Terminal.app 本身并不是 Ruby 解释器。您需要启动 irb 以获得交互式 Ruby 控制台:
有 其他技巧,您可以使用它在特定脚本的上下文中运行
irb
。Please note that Terminal.app is not itself a Ruby interpreter. You'll want to fire up
irb
to get an interactive Ruby console:There are other tricks you can use to run
irb
in the context of a particular script.首先,您需要运行 irb(或使用 ruby -e 将代码传递给解释器),因为终端不知道该代码块是什么或如何解释它。
之后,您应该能够按照您所说的将其粘贴进去来运行。
Well first you need to run irb (or pass the code to the interpreter using
ruby -e
) as the terminal doesn't have any idea what that block of code is or how to interpret it.After that you should be able to run in by pasting it in as you say.