Tuesday, 23 November 2010

7 Languages

People keep batting around the statement that you should aim to learn a new language every year to be a good programmer. This tends to be attributed to the book “The Pragmatic Programmer” the authors of which went on to create their own publishing company and have released many more books on programming pragmatically. One of their most recent releases is “Seven Languages In Seven Weeks” and in my aims to be a better dev, I have decided to attempt to follow this book and see what I can learn. It takes a bunch of languages that have fundamental differences between them, such as dynamic, functional and logic based, aiming to give you a basic understanding of what makes that language interesting and useful compared to the others. Whether or not I get to use any of these languages in my day job, hopefully it will open my eyes to different ways of doing things, and make me better with C# and VB.net.

The first language up is Ruby. I’ve never had the chance to get involved in Ruby at work, but have heard plenty of good things about the language so have been interested in learning a bit about it. I’m sure that there are plenty of other devs in the same boat, so this was a great choice of opener in my opinion.

Day 1 starts with a simple intro to Ruby syntax: simple assignments, printing to the console, the fact that everything is a object unlike .net with its value types, the way that the if statement considers 0 to be true, and touching on the dynamic typing but without going into enough depth to really get a feel for how this is useful in the real world. I hope to see days 2 and 3 help to get this point across as this is one of the key areas that I want to grok about Ruby coming from a static language like C#. There are plenty of small simple code examples in this segment that illustrate what is being taught as well as serving as a springboard for a little fiddling. They are all run in the irb REPL program. It’s quite nice to have such a light weight way to run some simple code. Visual Studio can be awesome, but it does take a while to get you to the code from a cold start.

After the usual discussions about how to do the basics the chapter leaves off with some exercises for the reader to perform. First of all these are just finding some useful sources of documentation on the intarwebs, including ranges. (‘aa’..’bb’).to_a is a very interesting thing to see in action :) After this there were a few exercises, starting off with the classic “Hello, world”, some basic looping, running code from a file rather than in the REPL and progressing through to a simple “guess the random number between 1 and 10 with a prompt to go higher or lower next go” game. I’m not sure that I’ll win any point for style, but these all came fairly simply. This last step of engaging us with some tasks based on, and stretching beyond, what has been given in the chapter works well for helping to solidify that learning.

This was a decent intro to the basics of the language and the approach of the book that has me looking forward to working through some more. For the record, and to open myself up to ridicule were anyone to bother with reading my little blog, the code I came up with for my guessing game was:

mynum = rand(10)
guess = -1
until guess == mynum
    puts "have a guess"
    guess = gets.to_i
    puts 'too low' if guess < mynum
    puts 'too high' if guess > mynum
end
puts 'woot'

I’m not sure what will happen if a non numeric character was entered, I don’t think Ruby is dynamic enough to come up with something that meaningful on its own ;)

No comments:

Post a Comment