30 Ruby Basic Exercises for Intermediate with Solutions

Master intermediate Ruby skills with our comprehensive list of top 30 exercises. Dive into coding challenges that improve your understanding and proficiency in Ruby , setting a solid foundation for advanced challenges. Start your journey to Ruby mastery today!

Learning Objectives:

Strengthen your understanding of Ruby by working with arrays, hashes, methods, and object-oriented programming. Improve problem-solving skills with intermediate challenges that require logical thinking and efficient coding practices.

Exercise Instructions:

  • Start with the first exercise and attempt to solve it before checking the hint or solution.
  • Ensure you understand the logic behind each solution, as this will help you in more complex problems.
  • Use these exercises to reinforce your learning and identify areas that may require further study.

1. Write a program to find the first non-repeating character in a string.

Required Input:

"swiss"

Expected Output:

w

Code In Ruby

# Predefined string string = "swiss" # Your logic here

Run Code?

Click Run Button to view compiled output

2. Implement a method to flatten a nested array without using the flatten method.

Required Input:

[[1, 2, [3]], 4]

Expected Output:

[1, 2, 3, 4]

Code In Ruby

# Predefined nested array nested_array = [[1, 2, [3]], 4] # Your logic here

Run Code?

Click Run Button to view compiled output

3. Write a method to generate all permutations of a given string.

Required Input:

"abc"

Expected Output:

["abc", "acb", "bac", "bca", "cab", "cba"]

Code In Ruby

# Predefined string string = "abc" # Your logic here

Run Code?

Click Run Button to view compiled output

4. Create a class Circle that calculates area and circumference, given the radius.

Required Input:

5

Expected Output:

Area: 78.54
Circumference: 31.42

Code In Ruby

# Predefined radius radius = 5 # Your logic here

Run Code?

Click Run Button to view compiled output

5. Write a program to check if a number is an Armstrong number.

Required Input:

153

Expected Output:

true

Code In Ruby

# Predefined number number = 153 # Your logic here

Run Code?

Click Run Button to view compiled output

6. Create a method to find the longest word in a sentence.

Required Input:

"The quick brown fox jumps over the lazy dog"

Expected Output:

quick

Code In Ruby

# Predefined sentence sentence = "The quick brown fox jumps over the lazy dog" # Your logic here

Run Code?

Click Run Button to view compiled output

7. Write a program to rotate an array n times to the right.

Required Input:

[1, 2, 3, 4, 5], 2

Expected Output:

[4, 5, 1, 2, 3]

Code In Ruby

# Predefined array and number of rotations array = [1, 2, 3, 4, 5] rotations = 2 # Your logic here

Run Code?

Click Run Button to view compiled output

8. Implement a binary search algorithm for a sorted array.

Required Input:

[1, 3, 5, 7, 9], 5

Expected Output:

2

Code In Ruby

# Predefined sorted array and target array = [1, 3, 5, 7, 9] target = 5 # Your logic here

Run Code?

Click Run Button to view compiled output

9. Write a program to check if a string is a valid IPv4 address.

Required Input:

"192.168.0.1"

Expected Output:

true

Code In Ruby

# Predefined string ip_address = "192.168.0.1" # Your logic here

Run Code?

Click Run Button to view compiled output

10. Create a class Rectangle with methods to calculate area and perimeter.

Required Input:

Length: 10, Width: 5

Expected Output:

Area: 50
Perimeter: 30

Code In Ruby

# Predefined dimensions length = 10 width = 5 # Your logic here

Run Code?

Click Run Button to view compiled output

ad vertical

1 of 3