如何将独立的 Ruby 脚本编译成独立的 AOT 编译的 macruby 可执行文件?
我想在 MacRuby 中编写一个可以分发的命令行工具。
macrubyc
is your friend. The simple hello world example, In a terminal:
chris$ echo 'puts("Hello, world!")' > hello.rb
chris$ macrubyc hello.rb -o hi
chris$ ./hi
Hello, world!
chris$ file hi
hi: Mach-O 64-bit executable x86_64
chris$
There are a few things to bear in mind, the most important being that the binary needs a macruby runtime to link against. On your machine you can just run the 'hi' programme as I did above, but when you're shipping it to macs without macruby installed you'll have to statically compile it into the executable.
See the macrubyc manpage for more details.
传统的方法是将命令行实用程序作为未编译的 ruby 脚本提供。如果你在它前面加上
#!/usr/bin/env macruby
并设置文件可执行文件
$ chmod a+x myfile.rb
然后从用户的角度来看,它只是运行,就好像它是一个二进制文件一样。