My friend Timo recommended the book “Seven Languages in Seven Weeks” to me a couple of months ago. During my parental leave I’ve finally found time to read -and more important- code the exercises.
The first language is Ruby. Ruby is object-oriented, dynamically and strongly typed. It supports duck typing and is a good fit for developing DSLs.
Mac OS X comes with Ruby 2.0.0-p648 preinstalled. A bit rusty but should work for the books content. The interactive console can be invoked with the command
$ irb irb(main):001:0>
Exercises
The famous Hello World is pretty simple:
puts 'Hello World!'
A loop can be constructed with while:
x = 0 while x < 10 x = x + 1 puts "This is sentence number #{x}" end
For string extrapolation you have to use the double quotes.
Ruby has some nice string methods e.g. getting index of character sequences:
hello_ruby = "Hello Ruby!" puts hello_ruby.index("Ruby")
A simple guessing game looks like this:
while true random_number = rand(10) puts "Guess the number!" while true guess = gets().to_i puts "Too low" if guess < random_number puts "too high" if guess > random_number if guess == random_number puts "Correct!" break end end end