In the world of programming languages, sometimes you don't need the overhead and performance of Java, C#, C++, and other power tools. Sometimes a scripting language, or Swiss army knife, will do.
Of Perl, Python, and Ruby, only one was built from the ground up to combine scripting with object-oriented programming. If you don't know one of them, let's start with Ruby. (If you know Perl or Python, don't worry; that will make Ruby even easier to learn.)
[ Work smarter, not harder -- download the Developers' Survival Guide from InfoWorld for all the tips and trends programmers need to know. | Keep up with the latest developer news with InfoWorld's Developer World newsletter. ]
In this article, we'll cover how to install and run a Ruby project, as well as the fundamentals of Ruby syntax and style. You'll leave knowing how to run programs to do simple file I/O or Web access and it will take about the length of a lunch break.
Getting started with Ruby right now
You can download an open-source Ruby interpreter on ruby-lang.org. After getting the installer, let's create a program called hello.rb in a text editor. Here's the code:
print "What is your name? "
name = gets
name = name.chomp() #removes the CRLF from name
if name == "cheesy"
puts "You don't need this tutorial!"
puts "Hello " + name
The code looks a bit like BASIC and, to an extent, it might be. There are no parenthesis unless you want them, no semi-colons at the end of the line and, though curly braces can enclose a block, we generally use do/end.
A bit about Ruby style
We could keep going, showing syntax of Ruby semantics of Ruby when to do a for loop, for example, and how to add two numbers together. If you're like me, you'll end up writing a program that happens to be Ruby but looks very much like your programming language of choice. While we don't have time here to explore Ruby style in much depth, let me suggest one common approach to Ruby scripting.
Instead of solving the programming problem in the main routine, write an object. The main routine creates the object, passes input parameters into it, and perhaps prints something out. Once you've developed that habit, it's easy enough to put the objects in a code library and learn how to unit test that library.
For now, let's look at variables and control flow.
Variables: Strings, integers, floats, and arrays
Ruby is a strongly typed language, with integers, Boolean, decimal numbers and strings as basic data types. Data type is assigned implicitly; in the code below, "name" is a string, "val" a number, and "inte" an integer:
name = gets
val = 5.5
inte = 5
If you type "5" when "name" is requested, then try to add "inte" and "name," you'll get a conversion error. Although "name" is a string, it's also an object, and has a ".to_i()" function that returns an integer. Putting the code together looks like this:
added = name.to_i() + inte
Ruby uses other functions as well; "to_s()" converts to string, for example, and "to_f()" to float.
Meanwhile, there are a lot of ways to create and access an array in Ruby. For now, here are three:
array1 = array.new(10) #Creates a size-ten empty array
array2 = array[1, 2, 3, 4,5] #Array of 5 elements that increment