Solving Problems with Ruby

Apply your knowledge of Ruby by tackling a series of exercises on your own.

Scroll down...




You should have a pretty good handle on the basics of using Ruby by now, though you may feel unsure of all the different methods that are available to you and when to use them. We promise that you'll only actually end up using a handful of them in the real world so it won't be nearly as confusing later on.

For this assignment, we want to make sure you really understand Ruby and give you a chance to solve some problems with it on your own. The first section will have you go through an existing resource which is laid out with a series of small challenges and then the second part will have you solving some trickier (and classic) problems on your own.

The Ruby Monk

The last introductory-level assignment for you is to conquer the Ruby Monk's Introduction to Ruby. This is a fairly comprehensive and fast-moving course. Some of it will overlap what you already know and other parts may surprise you. If you're totally comfortable with Ruby by now, you can skip it.

  1. Do the Ruby Monk Primer. They will basically teach you something and then expect you to run a bit of code to make it happen. It takes a bit of getting used to where you're supposed to type your answers and how to run them.

Note: Ruby Monk is currently unavailable and can be accessed from this archived version for the time being.

Project Euler

Project Euler is a site containing a series of increasingly difficult programming problems. Many of the later ones are designed to be solved algorithmically instead of using a brute force method (some of them would take your computer 100 years to finish if you didn't find the "sneaky" way to solve it). The early ones, though, are great exercises for practicing to use Ruby for solving problems.

Some of these will require you to dust off a bit of math but if you're too lost in thinking mathematically, you're probably not doing it right -- let the computer do all the work for you and just use iteration and conditional logic to your advantage. This is a good test of "Can you find the simplest, dumbest possible solution or will you try to out-think the problem?" Remember KISS (Keep It Simple Stupid) is one of the principles of software engineering!

Write your solutions to the following problems in a .rb file and run it from the command line (Remember how? Does $ ruby ./yourfilename.rb sound familiar?):

  1. Problem 1: Multiples of 3 and 5
  2. Problem 2: Even Fibonacci Numbers
  3. Problem 3: Largest Prime Factor (Don't bother for numbers over 100,000 when using the brute force solution or you'll need a few days to calculate...)
  4. Problem 4: Largest Palindromic Product. For this one, make sure you're using the brute force solution! You'll want to figure out how to check whether a number is "palindromic" and then iterate through all 3 digit number products looking for the largest one. Hint: Start with the big ones and go backwards!

Student Solutions

Sign up to track your progress for free

There are ( ) additional resources for this lesson. Check them out!

There are no additional resources for this lesson just yet!

Sorry, comments aren't active just yet!

Next Lesson: Numbers, Operators and Expressions