Posted by on in iDevelop | 0 comments

Yeah, I finally started on Ruby, more than a year after I considered learning it. ๐Ÿ™‚

Installing it was a breeze as I downloaded OneClick Ruby Installer for Windows which is an EXE file that installs Ruby painlessly. Took less than 5 minutes!! ๐Ÿ˜‰

Now starting on Ruby wasn’t difficult at all. The OneClick Ruby Installer installed two editors, FreeRIDE & SciTE Editor. Any of them can be used to code & compile Ruby, & they are both good. I first tried FreeRIDE, its written in Ruby itself. It looked OK, not as cool as other editors like Zend Studio or DreamweaverMX. But then I noticed, it was quite an older version of FreeRIDE. I downloaded the latest version of FreeRIDE, it weighs almost as much as the OneClick Ruby Installer, about 14MB. Then I noticed that the SciTE Editor bundled with the OneClick Ruby Installer was also quite outdated, so I downloaded & installed the latest version of SciTE Editor as well. ๐Ÿ˜‰

Cool, I won’t go in details of these editors as I’m not reviewing them, but I’d say one thing that should be of importance to anyone looking at a Ruby Editor, FreeRIDE is written in Ruby & has more features than SciTE Editor but its quite heavy, in size as well in startup & system resources, while SciTE Editor is quite lightweight, starts up in a flash!! ๐Ÿ™‚

Ok, now moving on, I wanted to run some basic Ruby code to see if it runs, so I opened SciTE Editor and wrote my first Ruby Code

  1. # Time to say Hello
  2. puts "Hello World";

and it worked, yay!! ๐Ÿ˜‰
(now the users of iG:Syntax Hiliter might’ve noticed the Ruby hiliting of code above, well guys, don’t worry, if you wanna have this file, you can download it from the GeSHi website, I’ve already sent the language file to Nigel for inclusion in next GeSHi release, and ofcourse it’ll be accompanied in the next plugin release as well)

Pretty cool I’d say, so onto next one, I tried this code

  1. #lets try an array
  2. myArray = {
  3.     "first" => "Amit",
  4.     "last" => "Gupta"
  5. };
  7. puts "My name is " + myArray['first'] + " " + myArray['last'];

and viola, it worked pretty cool, returning the output

My name is Amit Gupta

Now some of you must be wondering why I’m so excited, well, I’m always like this when I try something new & it works in first attempt. It may not be a big deal for you but it is for me!! ๐Ÿ˜‰

Now I wanted to install RubyOnRails as well. I found a nice tutorial on installing Rails on Windows which is easy if you are more attentive!! ๐Ÿ˜‰ I failed twice & had to un-install & re-install Ruby 2 times!! ๐Ÿ™ So if you are a bit of dunderhead like me(or worse) then here’s what I observed, which you should follow, besides the instructions in that tutorial. After you’ve installed FastCGI & arrived at the point of “Update the Gem package“, then you should proceed like this:

  1. Open the Command Window and navigate to the “bin” folder where you installed Ruby. I installed it in the D: drive, so I entered the following at my DOS prompt:
    1. cd d:/ruby/bin/
  2. Once you are in the Ruby/bin, enter the following command to update the Gem package
    1. gem update
  3. Now it’ll start looking for the updates. It then presented me with a list of packages and asked me to choose one. I chose the package with highest number & the one which was tagged as mswin32. Just enter the serial number(will most probably be 2) of it and press ENTER. It’ll start off the download & it’ll take a bit of time depending on your connection speed. After that it’ll install the updates.
  4. After the updates are installed, its time to download & install Rails. So at the DOS prompt, enter the following command:
    1. gem install rails --include-dependencies

    This will start the Rails download & after its downloaded, it’ll be installed, you don’t need to do anything!! ๐Ÿ˜‰

Now that was quite easy & cool, no? Actually the tutorial I linked to, it missed just one step, the one of selecting the version of Gem to install.

As for running RubyOnRails, well, I haven’t done much yet but I ran a couple of simple examples & they worked!!

Wanna get started? Yeah, so lets move on as how I did it. I assume that you’ve Apache v2 installed in “D:/apache2/” with FastCGI enabled, and your document root is “D:/apache2/htdocs/”

  1. Open a Command Prompt and navigate to the Ruby’s bin directory. Enter the following command
    1. rails d:/apache2/htdocs/MyFirstRails

    This will create a Rails application by name of “MyFirstRails” at the above entered path, it’ll create the “MyFirstRails” sub-directory in the “htdocs” directory. If you open the “MyFirstRails” directory, you’ll see some sub-directories in it along with some files, which ofcourse RubyOnRails created!! ๐Ÿ˜€

  2. Now in the “public” sub-directory of “MyFirstRails” directory, you’ll see a .htaccess file, open it & find the line
    1. RewriteRule ^(.*)$ dispatch.cgi [QSA,L]

    and change it to

    1. RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
  3. Now open the file “dispatch.fcgi” and change the first line to the path of your ruby installation, which for me is as
    1. #!d:/ruby/bin/ruby

    save and close the file.

  4. To see if you’ve done OK so far, in the command prompt, naigate to the “MyFirstRails” sub-directory in your Apache “htdocs” directory and enter the following command
    1. ruby scriptserver

    This will start the WEBrick server that comes with Ruby & it default runs on port 3000. So navigate to http://localhost:3000/ and you should see a page open saying “Congratulations, you are on Rails!“. If you see this page, then you have done OK so far, else you might’ve goofed up. I’ll assume that you did OK so far & proceed(since I did OK & as I’m new to this, I don’t know how to troubleshoot, so don’t even ask). ๐Ÿ˜€

  5. Now, lets do some fun stuff, ok, so open another command prompt(let the WEBrick one remain open as if you close it, you’ll also close the WEBrick server & won’t be able to run your Rails application) and navigate to your “MyFirstRails” directory. Enter the following command
    1. ruby scriptgenerate controller MyTest

    This will create “my_test_controller.rb” file in the “app/controllers/” sub-direcory or your “MyFirstRails” directory. Open this file and it’ll have the following code in it

    class MyTestController < ApplicationController end [/ruby]

  6. Now we need to add something to it to see the results, as this is just a skeleton contoller file at the moment, so add the following code, so that the code in the file looks like

    class MyTestController < ApplicationController def index render_text "Hello World!!" end end [/ruby] Now save the file & navigate to http://localhost:3000/MyTest/
    You will see the output “Hello World!!” there, right? A thing to notice is that in the URL you navigated to, the end is the same as your controller name!! ๐Ÿ™‚
    We add another method to test, so the code in the “my_test_controller.rb” file becomes

    class MyTestController < ApplicationController def index render_text "Hello World" end def amit render_text "Hey Amit, you are uber cool!!" end end [/ruby] Now if you navigate to http://localhost:3000/MyTest/amit/ you will see the output “Hey Amit, you are uber cool!!”!! ๐Ÿ˜‰ Cool, so adding another method to the controller means that its accessible via appending its name to the controller name!! Btw, you can also access the index method that we created in the same way, but what’s the use!! ๐Ÿ˜‰

This really looks quite easy but then these were just some very very basic examples, something more complex, like creating a DB powered app would be very much cool. So I’ll try to do that now!! ๐Ÿ˜‰

And if you need a tutorial on creating a DB based application with RubyOnRails, you can try the cool O’Reilly article published on OnLamp,
Rolling With Ruby on Rails — Part I & Part II

Go Rails Go!! ๐Ÿ˜‰