From: "Jörg W Mittag" Date: 2010-05-31T00:25:19+09:00 Subject: Re: .rb security Ed Gallagher wrote: > Wondering if possible to compile a ruby script to byte code that can be > executed in command line just as a .rb Short answer: No, it doesn't. (Slightly) longer answer: No, *Ruby* doesn't support storing and loading bytecode, but most Ruby *Implementations* do. IOW: it's not part of the Ruby Language Specification, but most Ruby Implementations support it anyway. However, each Implementation has its own bytecode, its own archive format and its own APIs. And, of course, some implementations don't even *have* bytecode, so it's obviously impossible for them to support bytecode loading. (Much too) long answer: There is no portable bytecode specification for Ruby, and thus also no standard way to load precompiled bytecode archives. However, almost all Ruby implementations use some kind of bytecode or intcode format, and several of them can dump and reload bytecode archives. YARV () always compiles to bytecode before executing the code, however that is usually only done in memory. There are ways to dump out the bytecode to disk and to read it back *in* (). Rubinius () also always compiles to bytecode, and it has a format for compiled files () ('.rbc' files, analogous to JVM '.class' files). XRuby () is a pure compiler, it compiles Ruby sourcecode straight to JVM bytecode ('.class' files). You can deploy these '.class' files just like any other Java application. JRuby () started out as an interpreter, but it has both a JIT compiler and an AOT compiler () ('jrubyc') that can compile Ruby sourcecode to JVM bytecode ('.class' files). Also, work is underway to create a *new* () compiler that can compile (type-annotated) Ruby code to JVM bytecode that actually looks like a Java class and can be used from Java code without barriers. Ruby.NET () is a pure compiler that compiles Ruby sourcecode to CIL bytecode (PE '.dll' or '.exe' files). You can deploy these just like any other CLI application. MacRuby () doesn't allow you to compile to bytecode, but it allows you to compile to *native* code. IronRuby () also compiles to CIL bytecode, but typically does this in-memory. However, you can pass commandline switches () to it, so it dumps the '.dll' and '.exe' files out to disk. Once you have those, they can be deployed normally, just like any other PE executable wherever you have an installation of .NET or Mono. BlueRuby () automatically pre-parses Ruby sourcecode into BRIL (BlueRuby Intermediate Language), which is basically a serialized parsetree. (See *Blue Ruby - A Ruby VM in SAP ABAP* () for details.) I *think* (but I am definitely not sure) that there is a way to get Cardinal () to dump out Parrot () bytecode archives (PBC). (Actually, Cardinal only compiles to PAST, and then Parrot takes over, so it would be Parrot's job to dump and load bytecode archives.) MRI doesn't have a bytecode. HotRuby () and Red Sun () appear to have the same bytecode format as YARV, and they appear to be able to load bytecode, but I am not familiar with them and can't make a definitive statement. I am not familiar enough with tinyrb (), RubyGoLightly (), SmallRuby () or MagLev () to make any statement about them. jwm