Tuesday, August 23, 2005

A Sample Ruby Code

A few people were asking for a sample Ruby code, so here goes!

First the history - my Babylon Time Trial expired and I was looking for a good alternative with my GRE coming up. So I thought, why not code it in Ruby? So basically what my code does is, it queries the Google Search engine, uses the 'define' keyword and retrieves the page containing the definitions of the word entered at the command line. After retrieving, I use some Regular Expressions (here's where Ruby makes life sooo easy!!) to parse the page and remove the unnecessary html tags, etc and display just the definitions. The code follows ...

require 'net/http'

word = ''
text = ''
ARGV.each {|arg| word << arg; word <<" "}

res = Net::HTTP.get_response('www.google.co.in', '/search?hl=en&q=define%3A'+word+'&btnG=Google+Search&meta=')

arr = res.body.split(/<\w+>/)
arr2 = []
arr.each { |g| arr2.push(g) unless g =~ /<.+>/}
arr2.delete("")
arr2.delete_at(0)
arr2.pop if arr2.last =~ /Display definitions/
arr2.clear if arr2.last =~ /all words are spelled correctly/
puts "\n#{arr2.length} Definition(s) of #{word.upcase}\n\n"
arr2.each { |d| puts d+"\n\n"}


A brief explanation of the Code -
The first line includes the net/http module.
The third line take the word(s) entered at the command line and forms a string.
The next line retrives the page using the 'define' keyword at google.
The rest of the code is just parsing the page to remove tags etc.

Here's a sample output

C:\Ruby>ruby google.rb behoove

1 Definition(s) of BEHOOVE

be appropriate or necessary; "IT behooves us to reflect on this matter"

C:\Ruby>ruby google.rb ruby programming language

1 Definition(s) of RUBY PROGRAMMING LANGUAGE

Ruby is an object-oriented programming language. It combines syntax inspired by Ada and Perl with Smalltalk-like object-oriented features, and also shares some features with Python, Lisp and CLU. Ruby is an interpreted language.


I think this code shows just some of the power of Ruby - in very few lines of code, we can achieve some AWESOME functionality! Till the next post ...

P.S. Im running a server at my place, check it out at http://swapneel.no-ip.info

Its got loads of Ruby and Java Code, and i have also deployed a Ruby on Rails application on it (thats gonna be the topic of my next post). No URLs for the former, good luck searching for it!
The latter can be found at http://swapneel.no-ip.info/code

Friday, August 05, 2005

Ruby: Programmer's Best Friend

I still cant remember how i got initiated into Ruby, but it definitely was a perfect example of serendipity! I do remember though, downloading and reading Programming Ruby aka the "The PickAxe" by Dave Thomas et al, and i was BLOWN AWAY!

Ruby is an Open Source Object Oriented programming/scripting language created by Yukihiro Matsumoto, a.k.a. “Matz” from Japan. In the foreward to the book, he writes -

"Man is driven to create; I know I really love to create things. And while I’m not good at painting, drawing, or music, I can write software.

Shortly after I was introduced to computers, I became interested in programming languages. I believed that an ideal programming languagemust be attainable, and I wanted to be the designer of it. Later, after gaining some experience, I realized that this kind of ideal, all-purpose language might be more difficult than I had thought. But I was still hoping to design a language that would work for most of the jobs I did everyday. That was my dream as a student.

Years later I talked with colleagues about scripting languages, their power and possibility. As an object-oriented fan for more than fifteen years, it seemed to me that OO programming was very suitable for scripting too. I did some research on the ’net for a while, but the candidates I found, Perl and Python, were not exactly what I was looking for. I wanted a language more powerful than Perl and more object-oriented than Python.

Then, I remembered my old dream and decided to design my own language. At first I was just toying around with it at work. But gradually it grew to be a tool good enough to replace Perl. I named it Ruby—after the precious red stone—and released it to the public in 1995.
Since then a lot of people have become interested in Ruby. Believe it or not, Ruby is actually more popular than Python in Japan right now. I hope that eventually it will be just as well received all over the world.

I believe that the purpose of life is, at least in part, to be happy. Based on this belief, Ruby is designed to make programming not only easy but also fun. It allows you to concentrate on the creative side of programming, with less stress. If you don’t believe me, read this book and try Ruby. I’m sure you’ll find out for yourself."

Ruby does all that Matz said it would and more! n lil wonder then, that its more popular than Python in Japan. The only surprising thing is why it hasnt caught on all over the world...im pretty sure it will. Any good programmer needs to just work with Ruby for a few days and realise why its sooo much better than most of the other programming languages!

I told Hrishikesh ("thite" as we more commonly call him) bout Ruby and he too was HOOKED. Gautam (aka "geek") meawhile was learning Python.

So what is it that makes Ruby so gr8? Dave Thomas, in the preface of the book says -

"When Andy and I wrote the first edition, we had to explain the background and appeal of Ruby. Among other things, we wrote “When we discovered Ruby, we realized that we’d found what we’d been looking for. More than any other language with which we have worked, Ruby stays out of your way. You can concentrate on solving the problem at hand, instead of struggling with compiler and language issues. That’s how it can help you become a better programmer: by giving you the chance to spend your time creating solutions for your users, not for the compiler.”"

Having used Ruby for quite some time now, i havta agree. Whenever im writing code in Ruby, most of the thinking has gone into how to solve the problem rather than being stuck with the deficiencies of the language and how to get around them!

Ruby is a dynamically typed language (more on this in a later blog). Coming from a background of statically typed languages like C, C++, Java, etc. dynamic typing is soo beautiful, i wonder why ALL languages dont have it! Dave Thomas et al call it "Duck Typing" and say -

"In Ruby, the class is never (OK, almost never) the type. Instead, the type of an object is defined more by what that object can do. In Ruby, we call this duck typing. If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it were a duck."

Love that last line! :d

Well, i found duck typing pretty awesome as besides other advantages, it really helps incremental code development.

Another awesome resource for Ruby is Why's (Poignant) Guide to Ruby. Not only is it a fascinating read and a good source to learn Ruby, it also provides a unique take on Ruby. Read it to know more!

I hope those of you who read this blog feel tempted to give Ruby a try! Trust me itll change the way you code! Various downloads, source code, documentation, etc is available at the Ruby Home Page

Well, i think i should wind up this blog now! Examples of Ruby code and solutions to the Python Challenge in Ruby to follow shortly. Ill end by once again quoting Matz -

"The stone has started rolling. It will became a great mountain and fill the whole earth."
Yukihiro Matsumoto, a.k.a. “Matz”
Japan, August 2004
Preface to the Second Edition of "The PickAxe"