Seven Languages in Seven Weeks: Ruby Day 1

I recently read Seven Seven Languages in Seven Weeks by Bruce Tate. I found the book to be an interesting read and would recommend it to people wanting to do check out several languages in a short amount of time.

The book includes exercises for each of the seven programming languages it covers. I will post the solutions to some of these exercises here over the following days and weeks.

The book starts out with Ruby, a language I knew little about before reading it’s chapter. Ruby is a dynamic interpreted object-oriented language, popular for Ruby on Rails, a web application framework.

I must say I took a liking to Ruby.

The questions for day 1, were mostly basic stuff, so here it goes…

Print the string “Hello, world”

# question 1
puts “Hello, world”

For the string “Hello, Ruby”, find the index of the word “Ruby”.

# question 2
puts "Hello, Ruby".index("Ruby")

Print your name ten times.

# question 3
(1..10).each { puts 'Gilles Leblanc' }

Print the string “This is sentence number 1,” where the number 1 changes from 1 to 10.

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

Bonus problem: Have the player guess a random number and tell him if he picked too low or too high.

# bonus question 
random_number = rand(10)
user_input = gets.to_i
puts "Correct !" if user_input == random_number 
puts "Too low." if user_input < random_number
puts "Too high" if user_input > random_number

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s