我在 OS X 上使用 Ruby 1.8.7。Ruby解释器在哪里?我的目标是更多地了解 Ruby、解释语言和解释/解析。
3 回答
如果您在终端中键入,您可以运行which ruby
以找出将执行的 ruby 的位置。ruby
如果您想找到有关可执行文件的更多信息,可以运行:
$ ls -l $(which ruby)
lrwxr-xr-x 1 root wheel 76 Nov 8 12:56 /usr/bin/ruby -> ../../System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby
也就是说,执行which ruby
,并将其结果传递给ls -l
,这将告诉您它实际上是 Ruby 框架中二进制文件的符号链接。您还可以使用它file
来找出它是什么类型的文件:
$ file $(which ruby)
/usr/bin/ruby: Mach-O universal binary with 2 architectures
/usr/bin/ruby (for architecture x86_64): Mach-O 64-bit executable x86_64
/usr/bin/ruby (for architecture i386): Mach-O executable i386
如果您想确保从脚本执行用户路径中的 ruby,而不是硬编码 Ruby 所在的位置,您可以在脚本顶部使用以下解释器指令:
#!/usr/bin/env ruby
之所以可行,是因为几乎所有现代系统都有一个可执行文件,/usr/bin/env
可以根据您的路径执行您传递给它的实用程序;/usr/bin/ruby
因此,您可以让env
搜索路径为您搜索,而不是硬编码到您的脚本中。
whereis ruby
在终端窗口中会告诉你
You should find it under
System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby
and symlinked to
/usr/bin/ruby
.
running which ruby
will give you the exact location of the ruby being used if there are one or more implementations on your system.