# .rb security

**URL:** <https://www.ruby-forum.com/t/rb-security/189336>\
**Category:** Ruby\
**Created:** [May 28, 2010, 11:17pm UTC](https://www.ruby-forum.com/t/rb-security/189336 "2010-05-28T23:17:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![egonzalez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f08c70/32.png) [@egonzalez](https://www.ruby-forum.com/u/egonzalez)\
**Post date:** [May 28, 2010, 11:17pm UTC](https://www.ruby-forum.com/t/rb-security/189336/1 "2010-05-28T23:17:54Z")

</div>

Hi All,

Wondering if possible to compile a ruby script to byte code that can be  
executed in command line just as a .rb

Similar to that of .pyc for a .py

Is this possible in Ruby.

Thanks, Ed

---

<div class="post-metadata">

**Author:** ![egonzalez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f08c70/32.png) [@egonzalez](https://www.ruby-forum.com/u/egonzalez)\
**Post date:** [May 29, 2010, 12:05am UTC](https://www.ruby-forum.com/t/rb-security/189336/2 "2010-05-29T00:05:38Z")

</div>

On May 28, 6:18 pm, Ed Gallagher [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi All,
> 
> Wondering if possible to compile a ruby script to byte code that can be  
> executed in command line just as a .rb

You mean encrypt/lock your source code?

No, Ruby do not provide that functionality.

However, you can use things like tar2rubyscript and similar.

Or package things as executables using Ocra or exerb

Or using JRuby, compile everything into a jar file.

---

<div class="post-metadata">

**Author:** ![egonzalez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f08c70/32.png) [@egonzalez](https://www.ruby-forum.com/u/egonzalez)\
**Post date:** [May 29, 2010, 1:11am UTC](https://www.ruby-forum.com/t/rb-security/189336/3 "2010-05-29T01:11:45Z")

</div>

Il 29/05/10 00.05, Luis L. ha scritto:

> Or using JRuby, compile everything into a jar file.

Luis, i think you forgot to say: “And unless you compile the rb into a  
class, you just pack everything into a zip file with another extension”  
🙂 🙂

> –  
> Luis L.

- Francesco

---

<div class="post-metadata">

**Author:** ![egonzalez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f08c70/32.png) [@egonzalez](https://www.ruby-forum.com/u/egonzalez)\
**Post date:** [May 30, 2010, 5:26pm UTC](https://www.ruby-forum.com/t/rb-security/189336/4 "2010-05-30T17:26:24Z")

</div>

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 ([http://Ruby-Lang.Org/](http://Ruby-Lang.Org/)) 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_  
([https://GitHub.Com/Ruby/Ruby/blob/trunk/iseq.c#L438-534](https://GitHub.Com/Ruby/Ruby/blob/trunk/iseq.c#L438-534)).

Rubinius ([http://Rubini.us/](http://Rubini.us/)) also always compiles to bytecode, and  
it has a format for compiled files  
([https://GitHub.Com/EvanPhx/Rubinius/blob/master/lib/compiler/compiled\_file.rb](https://GitHub.Com/EvanPhx/Rubinius/blob/master/lib/compiler/compiled_file.rb))  
(‘.rbc’ files, analogous to JVM ‘.class’ files).

XRuby ([http://XRuby.GoogleCode.Com/](http://XRuby.GoogleCode.Com/)) 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 ([http://JRuby.Org/](http://JRuby.Org/)) started out as an interpreter, but it has  
both a JIT compiler and an AOT compiler  
([https://GitHub.Com/JRuby/JRuby/tree/master/src/org/jruby/compiler/](https://GitHub.Com/JRuby/JRuby/tree/master/src/org/jruby/compiler/))  
(‘jrubyc’) that can compile Ruby sourcecode to JVM bytecode (‘.class’  
files). Also, work is underway to create a _new_  
([https://GitHub.Com/JRuby/JRuby/blob/master/tool/compiler2.rb](https://GitHub.Com/JRuby/JRuby/blob/master/tool/compiler2.rb))  
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](http://Ruby.NET) ([http://RubyDotNETCompiler.GoogleCode.Com/](http://RubyDotNETCompiler.GoogleCode.Com/)) 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 ([http://MacRuby.Org/](http://MacRuby.Org/)) doesn’t allow you to compile to  
bytecode, but it allows you to compile to _native_ code.

IronRuby ([http://IronRuby.Net/](http://IronRuby.Net/)) also compiles to CIL bytecode, but  
typically does this in-memory. However, you can pass commandline  
switches  
([https://GitHub.Com/IronRuby/IronRuby/blob/master/Languages/Ruby/Ruby/Hosting/RubyOptionsParser.cs#L419-420](https://GitHub.Com/IronRuby/IronRuby/blob/master/Languages/Ruby/Ruby/Hosting/RubyOptionsParser.cs#L419-420))  
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  
([https://SDN.SAP.Com/irj/scn/wiki?path=/display/Research/BlueRuby](https://SDN.SAP.Com/irj/scn/wiki?path=/display/Research/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_  
([https://SDN.SAP.Com/irj/scn/go/portal/prtroot/docs/library/uuid/408a9a3b-03f9-2b10-b29c-f0a3374b19d8](https://SDN.SAP.Com/irj/scn/go/portal/prtroot/docs/library/uuid/408a9a3b-03f9-2b10-b29c-f0a3374b19d8))  
for details.)

I _think_ (but I am definitely not sure) that there is a way to get  
Cardinal ([https://GitHub.Com/Cardinal/Cardinal/](https://GitHub.Com/Cardinal/Cardinal/)) to dump out Parrot  
([http://ParrotCode.Org/](http://ParrotCode.Org/)) 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 ([http://HotRuby.Accelart.Jp/](http://HotRuby.Accelart.Jp/)) and Red Sun  
([https://GitHub.Com/JonathanBranam/RedSun/](https://GitHub.Com/JonathanBranam/RedSun/)) 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  
([http://Code.MACournoyer.Com/tinyrb/](http://Code.MACournoyer.Com/tinyrb/)), RubyGoLightly  
([http://Feyeleanor.GitHub.Com/RubyGoLightly/](http://Feyeleanor.GitHub.Com/RubyGoLightly/)), SmallRuby  
([http://SWING.FIT.CVUT.Cz/projects/smallruby/](http://SWING.FIT.CVUT.Cz/projects/smallruby/)) or MagLev  
([http://MagLev.GemStone.Com/](http://MagLev.GemStone.Com/)) to make any statement about them.

jwm

---

<div class="post-metadata">

**Author:** ![egonzalez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f08c70/32.png) [@egonzalez](https://www.ruby-forum.com/u/egonzalez)\
**Post date:** [June 10, 2010, 7:37am UTC](https://www.ruby-forum.com/t/rb-security/189336/5 "2010-06-10T07:37:31Z")

</div>

2010/5/30 JÃ¶rg W Mittag [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> JRuby ([http://JRuby.Org/](http://JRuby.Org/)) started out as an interpreter, but it has  
> both a JIT compiler and an AOT compiler  
> ([https://GitHub.Com/JRuby/JRuby/tree/master/src/org/jruby/compiler/](https://GitHub.Com/JRuby/JRuby/tree/master/src/org/jruby/compiler/))  
> (‘jrubyc’) that can compile Ruby sourcecode to JVM bytecode (‘.class’  
> files). Also, work is underway to create a _new_  
> ([https://GitHub.Com/JRuby/JRuby/blob/master/tool/compiler2.rb](https://GitHub.Com/JRuby/JRuby/blob/master/tool/compiler2.rb))  
> 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.

This has largely landed in 1.5.1, with improvements to follow in 1.6 I’m  
sure.

The following Ruby code:

class Foo  
def initialize(a, b); @a, @b = a, b; end  
def foo(a); @a + a; end  
def self.bar(a); a \* a; end  
end

Compiles to this Java class (just outlined here):

~/projects/jruby âž” jrubyc --javac foo.rb  
Generating Java class Foo to /Users/headius/projects/jruby/Foo.java  
javac -d /Users/headius/projects/jruby -cp  
/Users/headius/projects/jruby/lib/jruby.jar:.  
/Users/headius/projects/jruby/Foo.java

~/projects/jruby âž” javap Foo  
Compiled from “Foo.java”  
public class Foo extends org.jruby.RubyObject{  
public static org.jruby.runtime.builtin.IRubyObject  
**allocate** (org.jruby.Ruby, org.jruby.RubyClass);  
public Foo(java.lang.Object, java.lang.Object);  
public java.lang.Object foo(java.lang.Object);  
public static java.lang.Object bar(java.lang.Object);  
static {};  
}

Adding type signatures to appease Java:

require ‘java’

class Foo  
java\_signature “Foo(int, String)”  
def initialize(a, b); @a, @b = a, b; end  
java\_signature “int foo(int)”  
def foo(a); @a + a; end  
java\_signature “String bar(String)”  
def self.bar(a); a + a; end  
end

Produces the following class:

Compiled from “Foo.java”  
public class Foo extends org.jruby.RubyObject{  
public static org.jruby.runtime.builtin.IRubyObject  
**allocate** (org.jruby.Ruby, org.jruby.RubyClass);  
public Foo(int, java.lang.String);  
public int foo(int);  
public static java.lang.String bar(java.lang.String);  
static {};  
}

Fun and simple 🙂

- Charlie
