Why would I learn this language?
Ruby is a both powerful and easy to learn. It has a syntax which feels natural and is also very flexible.
Ruby is surrounded by a passionate community and there are a wide range of 3rd party libraries available. This makes it easy to develop new applications by relying on existing work.
Ruby is a cross platform development language with multiple implementations, including MRI (the default implementation), JRuby, IronRuby and Rubinius.
Starting Points
- Try Ruby
- An online interactive tutorial.
- Learn to Program
- A complete introduction for programming using Ruby including advice for students and teachers.
- The Poignant Guide to Ruby
- A fun illustrated introduction to programming using Ruby
- Programming Ruby: The Pragmatic Programmer's Guide
- A tutorial and reference for the Ruby programming language.
Example Code
The following is an example of the 100 doors problem. You can run and edit this program here.
for n in 1..100
if (n % 3 == 0) && (n % 5 == 0)
puts "Fizzbuzz"
elsif n % 3 == 0
puts "Fizz"
elsif n % 5 == 0
puts "Buzz"
else
puts n
end
end
The following is an example of the 100 doors problem. You can run and edit this program here.
#!/usr/bin/env ruby
def doors n
# Initialize an array
doors = [false] * n
# Process the doors
(1..n).each do |inc|
(inc..n).step(inc) do |d|
doors[d] = !doors[d]
end
end
# Print out the results
doors.each_with_index do |b, i|
puts "Door \##{i} is #{b ? 'open' : 'closed'}."
end
end
# Call the doors function with n = 100
doors(100)
Further Reading
- Wikipedia: Ruby
- Ruby (Main Website)
- Ruby on Rails