What do you think about turning your Rails application into an executable?
As stated in the title: What do you think about turning your rails application into an executable? For example to show it to a client that has no environment (ruby, database) installed?
I just found that it is possible
To start we will need the following tools:
- Tar2RubyScript - this is a tool that turns your entire ruby application into only one .rb file.
- RubyScript2Exe - this is a tool that turns one ruby script into an executable (no cross compiling yet, if you use linux you can only create linux executables, if you use windows just windows executables), with a great advantage, it includes the interpreter and all the libraries your application need, and filters only the used core classes.
Let’s install them:
$sudo gem install tar2rubyscript $sudo gem install rubyscript2exe
Now let’s create a Ruby On Rails application, and as we want to carry everything with us, we will use the SQLite3 database (that way the client does not need to have a database server).
Creating the application
$rails demo $cd demo $ruby script/server
Now test the application pointing your browser to the address: http://localhost:3000/.
Creating the database
$irb
irb(main):005:0> require 'sqlite3'
irb(main):006:0> SQLite3::Database.new("demo_dev.db").execute(
irb(main):007:1* "create table books (id integer primary key,
irb(main):008:1" title varchar(255),
irb(main):009:1" author varchar(255));")
And now we copy the database to the production and test environments:
$cp demo_dev.db demo_test.db $cp demo_dev.db demo_prod.db
Developing the Rails application
$vi config/database.yml
Replace the content of this file with something like this:
development: adapter: sqlite3 database: demo_dev.db test: adapter: sqlite3 database: demo_tst.db production: adapter: sqlite3 database: demo_prd.db
Let’s create the model and the controller:
$script/generate model Book $script/generate controller Book $vim app/controllers/book_controller.rb
And now lets change the controller code to something like this:
class BookController < ApplicationController scaffold :book end
$script/server
Now try this address in your browser to make sure it is working http://localhost:3000/book.
Creating the Ruby Archive
Now the real tip (Now talking serious, even my cat knows how to create a CRUD with scaffold
)
The Tar2RubyScript command creates a package for the application, but when it is executed , it unpacks every thing in a temporary directory and after the execution it cleans the temporary directory again. This is not exactly the behavior we wait for a database, so let’s move the database to a secure place before the execution…
$vi config.environment.rb
Let’s add the following code to the begining o the file:
module Rails
class Configuration
def database_configuration
conf = YAML::load(ERB.new(IO.read(database_configuration_file)).result)
if defined?(TAR2RUBYSCRIPT)
conf.each do |k, v|
if v["adapter"] =~ /^sqlite3/
v["database"] = oldlocation(v["database"]) if v.include?("database")
v["dbfile"] = oldlocation(v["dbfile"]) if v.include?("dbfile")
end
end
end
conf
end
end
end
It will change the way Rails looks for the database, it originally was this way:
module Rails
class Configuration
def database_configuration
YAML::load(ERB.new(IO.read(database_configuration_file)).result)
end
end
end
What exactly we did? When a RBA (Ruby Archive) is executed, it needs to handle two diferent directories, the one where the application is running (temp directory) and the one where the application really is, referenced by “oldlocation”. The presented code changed the configuration inside the database.yml file, pointing the databases to the full path to the database files (where the application really is), this way we do not loose the data after the end of the application.
The Tar2RubScript needs a file called init.rb, what will be the entry point to the application, so let’s create it with the following code:
$vi init.rb
at_exit do require "irb" require "drb/acl" require "sqlite3" end load "script/server"
The ‘require’ in the beginning are there just to make the script’s work a little easier, and the load “script/server” will start the Rails server (WEbrick or mongrel depending on your environment).
Now lets create the application package:
$cd .. $tar2rubyscript demo $cp demo/demo_* .
Almost ready, now we can test our Ruby Archive with the following command:
$ruby demo.rb
And again, test your application with the following URL: http://localhost:3000/book
Creating an executable to carry with you in your flash drive (outside Brazil it is called pendrive too?)
Creating the executable is very simple, you just need to run this command line:
$rubyscript2exe demo.rb
After the rails application starts up, navigate a little within your application and then press CTRL+C, it will create a file names demo.exe, or demo_linux if you are running a linux box like me.
Now just copy the generated file and the database file to your flash drive and run it on your friends computer of your client meeting to make some money
Conclusions
When you get in the meeting you do not need to install Ruby, RubyGems, Rails, and other Gems, …
Just an executable file and some database files and you are ready to demo your application to clients and friends
What do you want better than that?
Talking the truth, I still want the possibility to create windows executables into my Linux box, but it still a very good solution ![]()
If you enjoyed this post, make sure you subscribe to my RSS feed!





[…] read more | digg story […]
[…] for new clients for example.I wrote the post, but the blog has only two days, I need some adhttp://www.urubatan.info/2007/10/what-do-you-think-about-turning-your-rails-application-into-an-exec…Blog Review: Grow Your Writing Business Get Paid to Write OnlineWhat??s inside the blog? Here are […]