15 May 2011

It’s funny because I never learned Ruby, I never wanted to because dynamic languages don’t move me much, but so far it’s been quite fun.

First, a link to http://ruby-doc.org/docs/ProgrammingRuby/ that is the main reference to every question on that language.

I’m not going to run through all the exercises, but I will put solutions of those I found interesting to solve.

Print “This is sentence number 1” where the number 1 changes from 1 to 10:

(1..10).each { |i| puts "This is sentence number #{i}" }

Bonus problem: write a program that picks a random number (I will do between 1 and 1024, how geeky!). Let the player guess a number, telling the player whether the guess is too low or too high:

max = 1024

guess = -1
number = rand(max) + 1

puts "Guess a number from 1 to #{max}"

until guess == number
  guess = gets.to_i
  puts "Number is greater than #{guess}" if number > guess
  puts "Number is lesser than #{guess}" if number < guess
end

puts "Well done!"


blog comments powered by Disqus