I recently decided I was going to take up Ruby programming as a hobby. Prior to that I had taken on Python, which has quickly become one of my favorite languages. Ruby is coming in a close second, and could easily take it over as soon as they have built in XML support – so I thought I’d follow up my Intro To Python series with some Intro To Ruby posts.
I’ve found Ruby very easy to learn and use – I decided to learn Ruby about 2 weeks ago now, and have since released Blosxonomy, a Ruby implementation of a Blosxom like blogging system that powers this site. Its syntax is simple and intuitive, and the grammar is expressive and easy to learn.
Ruby carries the tag line “Programmers’ Best Friend.” I don’t know if I’d go quite this far, but it’s certainly a very developer friendly language – most comparable to Python in my experience. Its syntax, according to ruby-lang.org is inspired by Eiffel and Ada, neither of which I’ve had the pleasure of working with – but as both a Python and Ruby newby (relatively speaking), I find them quite dissimilar. The language’s nuances are also very Pythonesque – as we’ll see in this post, and my Ruby copy of Building Strings using Lists, which will come in a day or two.
Having compared Ruby to Python thus far, I’d like to start by pointing out that Ruby, unlike Python, uses delimited code blocks. Python determines code blocks based on their indentation level in the source, which can cause issues when editors convert between tabs and spaces. Ruby doesn’t use an explicit statement-delimeter though, just a programmer-friendly newline. This combination eases development and allows developers use thier own coding style, which for seasoned developers is a solid positive, and for newbies a potential negative (for those of us that have to read your code, that is.)
Ruby, like Python, is both dynamically and strictly typed; meaning that a variable’s datatype is determined at runtime by its value; and that an objects type can not be changed.
So in any event, after a long winded birds eye view of my opinion of Ruby, I’ll show you a hello-world:
def sayHi( name ) puts "Hello, %s!" % String( name ) end sayHi( "Derf" )
def sayHi declares a function called sayHi. The parameter list is not required, but in this
case, we pass a single parameter called
name. Notice that the type of the
is not declared, that’s because it’s dynamically typed – meaning I could pass any object I want into the method.
To account for that fact, I use the built-in
String( x ) function to create a string from the given
instance, no matter what its type.
The body of the method uses the
puts method, which simply writes a string to standard-out followed by
a new line. The string is formatted using syntax identical to Python’s, using %s inside the string to represent some
arbitrary string variable, and passing in the arguments following a % symbol after the string. If you had more than
one argument, you’d pass them in a list by encapsulating the comma separated arguments in square-brackes [ ].
The code outside of the method declarations is the “main” part of the program. This is analogous to writing the “main” method of a C or C++ program – and identical to “global” code in a Python application.
So that’s the extreme basics and birds eye view of Ruby. My next post will be about Ruby’s built-in Arrays and Hashes (practically identical to Pthon’s), and I’ll also cover CGI support.
If you’re anxious to get started on your own though, I would recommend reading Programming Ruby: The Progmatic Programmer’s Guide online. If you’re very comfortable with programming languages in general, O’Reilly’s Ruby In A Nutshell provides a very thorough and technical reference – but it’s not a great book to learn from (a must have once you’re comfortable with the basics of the syntax and grammar though).