Thursday, June 14, 2012

Developing a Ruby on Rails Application on Windows

This is a "quick" overview of how I set up my development environment for Ruby on Rails on a Windows machine.

0) Uninstall any existing versions of Ruby

If this is your first time setting up the development environment you can skip this step, if you are trying to switch Ruby versions or are troubleshooting an issue you might already have Ruby installed and not care much about uninstalling it, maybe you have the Heroku toolbelt installed (if you are using that hosting service) which comes with its own Ruby installation (normally Ruby is installed at C:\Ruby, the Heroku toolbelt Ruby installation is at C:\Program Files (x86)\ruby).

Find any existing Ruby versions and uninstall them:
Control Panel\Programs\Programs and Features
(copy and paste the line above into the address bar of a Windows Explorer window)








After uninstalling delete the remaining Ruby folder and make sure the path was removed from the system variables to ensure a clean uninstall.

System variables path is found at:
Control Panel\System and Security\System

Click on 'Advanced system settings', then on 'Environment Variables' and then double click the path entry.
The path entry is composed of a single line with each item separated with a ;
A path that is added there will have its files available for programs and for you to execute without knowing their location (full path).



In the case of Heroku toolbelt I had one development machine that failed running 'bundle install' (more on this later) and the fix for it was removing the C:\Program Files (x86)\ruby item from the system variable path so it won't prevent the development kit (more on this later) files from being found when a gem installation required the build tools from the development kit that was installed at the C:\Ruby location...

1) Install Ruby

RoR applications are built with the Ruby language, this is the first step.

The latest Ruby version is available at:
http://rubyforge.org/projects/rubyinstaller/

If you need a specific version (your hosting service might require a specific version) you can get it from here:
http://rubyforge.org/frs/?group_id=167

2) Install RubyGems

Gems are libraries that are shared among Ruby projects, RubyGems helps us download and install those gems on the development machine where we installed Ruby.

Open a command prompt window (run C:\Windows\System32\cmd.exe) and type:
gem update --system
this should work because the C:\Ruby\bin path should be set in the system variables when we installed Ruby.



3)  Install Rails

This is the step that inspired me to write this blog post, if you follow the instructions at:
http://rubyonrails.org/download
then this step will fail.
You need to install the development kit, so lets try again.

3) Install Development Kit "One Click Install"

http://rubyinstaller.org/downloads/
Run the Development Kit "one click install" executable file, that will prompt you asking where to extract some files - point it to the path you installed Ruby at e.g. C:\Ruby

Go back to the command line and navigate to the Ruby installation folder, then type:
ruby dk.rb init
This will generate a file called config.yml

Now there is an optional step that could be useful if you are trouble shooting - type:
ruby dk.rb review
This will show you a list of Ruby installations that have been found - if this doesn't seem right you can fix it at the generated config.yml file.

Finally complete this step by running:
ruby dk.rb install

4) Install Rails

In the command prompt type:
gem install rails

Done.
Now you should be able to start developing rails applications, I would have liked to use Netbeans (my favorite IDE) but it seems that the RoR plugin for Netbeans doesn't run on the latest version... to get quickly up and running type in the command line:
gem install redcar
and then:
start redcar
This will get you started with a tool to edit the code and view the project files.
To generate a new project type in the command prompt:
rails new path/to/your/app

Gems that are required by your application are declared in the projects Gemfile, to install the gem dependencies of your application navigate to the projects folder in the command prompt and type:
bundle install

To run a local server to test your application, navigate to the application folder in the command prompt and type:
rails server
(can also type 'rails s' instead )
Once the server is ready Windows might prompt you about allowing it through the firewall.
Browsing to http://localhost:3000 should take you to your application.

Soon enough you will want to use a relational database, lets walk through setting one up - download PostgreSQL from:
http://www.postgresql.org/download/

A nice thing about PostgreSQL is that it comes with a GUI tool to set up and view databases, run the pgAdmin tool once installation is complete and add a login role and new databases for your application.

Right click 'Login Roles' to add a new role for your application to use to login to the database, the role name could be something like myAppRole.

Right click 'Databases' to add two new databases - one for development and one for testing, use the dropdown to set the Owner to be the role you created in the previous step. The databases names could be something like appname_development and appname_test.

A RoR application can use a PostgreSQL data base by using a gem called pg. Add the pg gem to your application Gemfile by adding this line to the Gemfile (found in your application folder):
gem 'pg'

Make sure all the gems your application needs are installed by navigating to the application folder and runing the command:
bundle install

To configure the database connection open appfolder\config\database.yml and set the adapter to postgresql, set encoding to unicode, set the database to the name of the data base you created with pgAdmin and the username and password to the role that you created with pgAdmin.
The top of the file could look like this:
development:
  adapter: postgresql
  encoding: unicode
  database: myApp_development
  username: myAppRoleName
  password: myAppRolePassword
  pool: 5
  timeout: 5000

This should be all you need to start using a relational database in your application... once you do, you will have migration files that could be used to setup the database with the tables your application needs - to do that navigate to the application folder and run the command:
rake db:setup

Good luck!

Python scripting with a java based engine

This is actually a very old post that I just forgot to publish, the engine that is mentioned is open source at:
http://code.google.com/p/cosmic-engine/

I had a very good experience integrating Jython with the java based engine I'm working on.
It is fairly straight forward to execute dynamic scripts that can receive java objects, create java objects and utilize existing java API... the first thing I used it for was embedding interaction scripts in objects.

(character behaviors triggered by a script that is embedded in objects: floor - go to, chair - go sit)

Picking (clicking with the mouse and checking collision with a ray shot form the camera) an object is a very natural way of interaction, embedding the script in the picked object makes the system very extensible, a message source (I use a class type) and message data (generic java object) that are passed to the script along with additional information about the pick serves as a form of a "loose hand shake" between the code that initiated the pick and the picked object script.

Initialization scripts could enable the team to set up testing environments and improve productivity,