Compile Ruby Script To Exe (MS DOS or Windows)

Today I spent some time compiling a small ruby script of mine into an executable. Why? well, I don’t have concerns of hiding the code but this was a requirement of one of the users of my script.

I started with a Google search on such compilers. RubyScript2Exe is what I used. After installing it as a gem I got an error. I downloaded the script RubyScript2Exe.RB and tried again. But fate had it – the same error.

C:\Users\mawasthi\Desktop>ruby rubyscript2exe.rb theboringstuff.rb
rubyscript2exe.rb:621:in `replace’: can’t modify frozen string (RuntimeError)
from rubyscript2exe.rb:577:in `block in newlocation’
from rubyscript2exe.rb:505:in `block in newlocation’
from rubyscript2exe.rb:472:in `newlocation’
from rubyscript2exe.rb:505:in `newlocation’
from rubyscript2exe.rb:577:in `newlocation’
from rubyscript2exe.rb:619:in `<main>’

I read about solutions on forums. None helped. Problem was clear (some change in Ruby updated version in which program name will be a frozen string).

Then I got from one of the forums a link to another “less SEO’ed” compiler “ocra”. I installed it –

C:\Users\mawasthi\Desktop>gem install ocra
Fetching: ocra-1.3.0.gem (100%)
Successfully installed ocra-1.3.0
1 gem installed
Installing ri documentation for ocra-1.3.0…
Installing RDoc documentation for ocra-1.3.0…

Worked like charm:

C:\Users\mawasthi\Desktop>ocra theboringstuff.rb
=== Loading script to check dependencies
:: === Including 52 encoding support files (2741248 bytes, use –no-enc to exclude)
=== Building theboringstuff.exe
=== Adding user-supplied source files
=== Adding ruby executable ruby.exe
=== Adding library files
=== Compressing 5678626 bytes
=== Finished building theboringstuff.exe (1391140 bytes)