Sunday afternoon with Ruby

I usually say Sunday is the creative day!

I looked around to see what creative thing I could be doing today. Ok, let’s learn another programming language. During the week I got hands on Computer Science Programming Basics with Ruby. The book consists of 188 pages perfect for some weekend reading. I was all set for a Ruby adventure. I had no previous experience with Ruby though. A perfect timing to learn some Ruby then.

I was kinda impressed the way the book starts. It doesn’t just dive into the language but instead introduces some basic programming concepts which are not boring at all. Maybe this is not something an advanced level programmer would require. Yet as the title suggests basic I believe the introduction part starts well.

I also liked the way Ophir Frieder, Gideon Frieder and David Grossman, the authors, introduce multi-dimensional array. I take the liberty to share an extract from the book to give you a better idea.

Arrays that have more than one dimension are called multidimensional arrays. A common multidimensional array is the two-dimensional array, which can be used to represent matrices and coordinate systems. Unlike some other programming languages, Ruby does not provide built-in support for multidimensional arrays. The way to work around this is to put an array inside an array, which is essentially what a multidimensional array is anyway.

Consider the example in the previous section of an array that stores a list of five testscores for a student. Now, what if you had students with the following scores?

Geraldo: 73, 98, 86, 61, 96  
Brittany: 60, 90, 96, 92, 77  
Michael: 44, 50, 99, 65, 10  

The best way to represent this data is to create one array with three indices and to have each index contain five elements. This is basically an array with three rows and five columns. To create such an array in Ruby, type in the following:

arr = [[73, 98, 86, 61, 96],  
      [60, 90, 96, 92, 77],
      [44, 50, 99, 65, 10]]

By typing in this assignment statement, you have created a 3 × 5 table where the first, second, and third rows represent Geraldo’s, Brittany’s, and Michael’s test scores, respectively. To access an individual score, use the format array[row][column]. So, if you wanted to know what Brittany scored on her third exam (remember, each index starts with 0, not 1), type:

puts "Brittany's Third Exam: " + arr[1][2].to_s  
The output should be:  
Brittany's Third Exam: 96  

Each chapter includes code examples to easily grasp the concept and I had fun to play with. While this book does not make you a Ruby expert it definitely will help a beginner understand & apply basic programming concepts through Ruby.

Share this post