Practice Problems


Make an earnest attempt at each problem before looking at the solution. These are real world problems as opposed to "busy work". They are likely to appear in the course of everyday work. The first problem is an example that we'll solve together.

Problem 0: Create a method that adds two numbers together.

# Solution 0

def add(a, b)
return a + b
end

p add(4, 6)
# => 10

Summary: We already know how to add numbers together with the " + " operator. However, in problem 0, we need to create a method that adds any two numbers together. The idea being that we do not know what numbers will be added at the time we define the method. For this reason, we define a method "add" and pass two parameters "a" and "b" as placeholders. We can refer to these placeholders from within our "add" method. We return the sum of the parameters and remember to end our code block with the keyword "end". To demonstrate the code works, we print out the result of passing the add method numbers 4 and 6.

Remember, it's easy to write and test code if you use a text editor that allows you to run your programs. For Sublime Text, use cmd + b to run the current file.


Learn Ruby First : 10 practice problems

Problem 1: Count all of the vowels in a given string.

Problem 2: Sort an array of words by their last letter. ( Ex. ['apple', 'cat', 'dog'] becomes ['apple', 'dog', 'cat'] )

Problem 3: Keep only the elements in an array that start with a vowel.

Problem 4: Count the total number of words in a file.

Problem 5: Return true if a string has any special characters, false otherwise (Define special characters as anything apart from [a-zA-Z0-9]).

Problem 6: Sort an array of objects by their values.

Problem 7: Count the number of elements in an array that are palindromes (spelled the same backwards, i.e. racecar, radar).

Problem 8: Use the 'HTTParty' library to send a GET request to an API endpoint. Use the url 'http://jsonplaceholder.typicode.com/posts/1' as the endpoint and print the value for title.

Problem 9: Get the domain name of an email address without the ".com". (Ex. [email protected] returns 'aol')

Problem 10: Solve the FizzBuzz problem. This is a common interview question designed to verify that a candidate can a.) follow simple instructions and b.) actually write code. Here is the problem:

  • Write a program that prints the numbers 1 - 100 to the standard output.
  • Replace each multiple of 3 with the word, "Fizz".
  • Replace each multiple of 5 with the word, "Buzz".
  • Replace multiples of both 3 and 5 with the word "FizzBuzz".

results matching ""

    No results matching ""