May 07, 2008

Cool things in Ruby

I have been using Ruby for quite some time now, but only now I begin to see what I missed...

It started when we began using WATIR for shooting HTTP transactions to test our application. WATIR is based on Ruby, activating Internet Explorer's COM object - I recommend it for quick useful scripting.

What caught my attention was the Ruby under WATIR - the scripts were easy to write, readable, and contained clever "tricks" that saved a lot of scripting time (I'll give examples later).

Well, as they say: "I came for WATIR and stayed for Ruby".


cc Tom Goskar

Ruby is a cute scripting language that gives a much nicer feeling from a usability perspective than perl. For starters, it is OO. It is very flexible in regard to syntax. It is rich with functionality and extensibility options.

Don't worry, there are down sides too (remember that I am only starting to understand Ruby, so there is a high chance I am BSing here). For example, I still do not have a convenient working environment for scripting and debugging. The editor that comes packed with Ruby for Windows (SciTE) colors keywords and lets you run the script from the editor, and the interactive Ruby command line (irb) is a nice option that let's you script and run interactively, but this is not enough for me - I am used to the rich coding experience of Eclipse, so what I did now is to install the Ruby plug-in for Eclipse, and I'll update later about how convenient it is.

Well, I want to give a few examples for why I like Ruby and why it is cool.

I like Ruby's arrays. They come with many services built in as methods and let you use the array as stack or queue with no effort. Also, you can add arrays, subtract them, keep only unique elements, sort, etc. - ultra convenient.
myArray = [1,2,3,4,5]
myOtherArray = [2,4]
myDiffArray = myArray - myOtherArray # result will be [1,3,5]

I also like Ruby's built-in iterators, allowing you to split anything (more or less) into pieces and operate on each piece until you do them all:
myArray = [1,2,3,4,5]
myArray.each do |element| # element isa variable that gets each array element by turn
puts element # puts prints to the console
end

Another thing I like about Ruby is its rich core library and even richer extensions collection, covering (probably) everything I need. As in other user contributed libraries, the quality and completeness is not guaranteed and you are required to work hard to differentiate the good things from the bad.

I just started climbing the learning curve here, so I'll update later on things I find.

If you want to see Ruby in action in a few minutes, do the Ruby in 20 minutes tutorial.

5 comments:

Rich said...

Aptana is a free great IDE. I've also used e-texteditor for us Textmate users that need to develop Watir Scripts on a Windows box.

You should also try require 'ruby-debug' in your scripts. Then just add "debbugger" in your line of could where you would like irb to popup and interact with your current browser.

http://datanoise.com/articles/2006/7/12/tutorial-on-ruby-debug

Ofer Prat said...

rich, thanks a million for the tips - I'll try them right away.

Ofer

Rich said...

You may need to make this change to get ruby-debug working :)

http://dev.rubyonrails.org/ticket/11601

Ofer Prat said...

Trying Aptana studio for Ruby development and enjoying it - thanks for the recommendation rich!

Katy said...

Ruby is a fun language indeed. I enjoyed my encounters with WATIR so much that I got it to be a major part of one of our testing simulation tools ...