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
[ruby]
# Time to say Hello
puts “Hello World”;
[/ruby]
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
[ruby]
#lets try an array
myArray = {
“first” => “Amit”,
“last” => “Gupta”
};
puts “My name is ” + myArray[‘first’] + ” ” + myArray[‘last’];
[/ruby]
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:
- 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:
[code]
cd d:/ruby/bin/
[/code] - Once you are in the Ruby/bin, enter the following command to update the Gem package
[code]
gem update
[/code] - 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.
- After the updates are installed, its time to download & install Rails. So at the DOS prompt, enter the following command:
[code]
gem install rails –include-dependencies
[/code]
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/”
- Open a Command Prompt and navigate to the Ruby’s bin directory. Enter the following command
[code]
rails d:/apache2/htdocs/MyFirstRails
[/code]
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!! π - Now in the “public” sub-directory of “MyFirstRails” directory, you’ll see a .htaccess file, open it & find the line
[code]
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
[/code]
and change it to
[code]
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
[/code] - Now open the file “dispatch.fcgi” and change the first line to the path of your ruby installation, which for me is as
[code]
#!d:/ruby/bin/ruby
[/code]
save and close the file. - 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
[code]
ruby scriptserver
[/code]
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). π - 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
[code]
ruby scriptgenerate controller MyTest
[/code]
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
[ruby]
class MyTestController < ApplicationController end [/ruby] - 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
[ruby]
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
[ruby]
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!! π