Compiled hello world with MacRuby


To celebrate the amazing work being done by Laurent Sansonetti on MacRuby here is a hello world using the new LLVM based compiler.

$ echo "p ARGV.join(' ').upcase" > hello_world.rb
$ macrubyc hello_world.rb -o macruby_says
$ ./macruby_says hello world
"HELLO WORLD"

Note that to achieve this result, you need to be using the experimental branch of MacRuby and have LLVM installed. (check the readme available in MacRuby’s repo).

Let’s quickly look at what we just did.
We created a single line ruby script that takes the passed arguments, joins them and print them uppercase.
Then, we compiled our script into a Mach-O object file and produce an executable.

Here is an extract from Laurent’s latest status report:

Produced executables embed all the compiled Ruby code as well as MacRuby, statically.
It can be distributed as is and does not depend on anything MacRuby or LLVM at runtime.
The Ruby source code is compiled into native machine code (same process as we do at runtime with the JIT compiler), so it’s also a good way to obfuscate the source code.
The final binary looks like an Objective-C binary (except that it’s larger)

Don’t expect to compile Rails just yet, it’s still in a preliminary stage.

The final release you should let you pick one of the 2 compilation modes:

* normal mode: full ruby specs, compile down to machine code and use LLVM at runtime. (recommended for desktop/server apps)
* full mode: no full ruby spec support, no runtime code generation, no LLVM. (“very light application and/or if the environment does not support runtime code generation” (hint-hint))

As you can see, MacRuby is moving forward and the experimental branch should soon move to trunk.


Similar Posts

, , ,

  1. #1 by Alexander - July 12th, 2009 at 12:30

    Cool, I am waiting for my first Mac to be shipped and the first cool thing I will probe – MacRuby. Oh no, I prefer TextMate to be first in this list :)

  2. #2 by victor - July 12th, 2009 at 12:47

    I’m waiting anxiously for 0.5 already… yum!

  3. #3 by Matthijs Langenberg - July 12th, 2009 at 16:48

    Too bad the output file is 13MB, but great technology. :-D

    • #4 by Laurent Sansonetti - July 12th, 2009 at 18:17

      The output file size will be somewhat reduced in the future, stay tuned :-)

      • #5 by Eloy Duran - July 13th, 2009 at 01:06

        Man, why can’t it be 31MB?! I like big fat apps…

  4. #6 by Matt Aimonetti - July 12th, 2009 at 18:01

    The 13Mb is due to all the stuff being compiled with your app. The “full compilation mode” should be considerably smaller. But honestly for desktop or server apps, I don’t think it the size is a big deal. And as Laurent said, the output size should be improved.

  5. #7 by Alistair Holt - July 12th, 2009 at 22:52

    Looking good. MacRuby is shaping up nicely.

  6. #8 by sgwong - July 13th, 2009 at 04:02

    Too bad, MacRuby only run on Mac :)

    • #9 by Matt Aimonetti - July 13th, 2009 at 07:59

      It only runs on Mac for now, I expect to see it running on other POSIX platforms later on.

  7. #10 by Michael Rykov - July 13th, 2009 at 12:23

    I would be curious to see some benchmarks comparing compiled MacRuby vs native ObjC (and maybe MRI for reference).

    When you’re writing a “very light application” for a “resource restricted environment”, a significant performance hit might not be worth Ruby’s elegance.

  8. #12 by roger - July 23rd, 2009 at 08:28

    So this doesn’t actually compile ruby, as much as bundle a ruby script into a single executable, I’m thinking [albeit faster since it's running on LLVM and not yarv]. I think that’s right.

  1. No trackbacks yet.

Comments are closed.