30 Ruby Basic Exercises for Beginners with Solutions
Master beginner 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 intermediate challenges. Start your journey to Ruby mastery today!
Learning Objectives:
Develop a strong foundation in Ruby syntax, data types, loops, and conditionals. Enhance problem-solving skills by implementing logic through beginner-friendly coding exercises.
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 Ruby program to check if a number is odd or even.
Required Input:
number = 7
Expected Output:
7 is odd
Code In Ruby
Run Code?
Click Run Button to view compiled output
2. Create a method that takes a string and returns the string reversed.
Required Input:
"hello"
Expected Output:
olleh
Code In Ruby
Run Code?
Click Run Button to view compiled output
3. Write a program that takes an array of integers and returns the sum of all elements.
Required Input:
[1, 2, 3, 4, 5]
Expected Output:
15
Code In Ruby
Run Code?
Click Run Button to view compiled output
4. Write a method to check if a given string is a palindrome.
Required Input:
"madam"
Expected Output:
true
Code In Ruby
Run Code?
Click Run Button to view compiled output
5. Create a method to calculate the factorial of a number.
Required Input:
5
Expected Output:
120
Code In Ruby
Run Code?
Click Run Button to view compiled output
6. Write a Ruby program to find the largest number in an array.
Required Input:
[3, 7, 2, 9, 4]
Expected Output:
9
Code In Ruby
Run Code?
Click Run Button to view compiled output
7. Create a method to sort an array of integers in ascending order.
Required Input:
[5, 2, 9, 1, 6]
Expected Output:
1
2
5
6
9
Code In Ruby
Run Code?
Click Run Button to view compiled output
8. Write a program to count the number of vowels in a given string.
Required Input:
"hello world"
Expected Output:
3
Code In Ruby
Run Code?
Click Run Button to view compiled output
9. Write a method to find the index of a specific element in an array.
Required Input:
Array: [10, 20, 30, 40, 50], Element: 30
Expected Output:
2
Code In Ruby
Run Code?
Click Run Button to view compiled output
10. Create a program to convert a string to uppercase.
Required Input:
"ruby programming"
Expected Output:
RUBY PROGRAMMING
Code In Ruby
Run Code?
Click Run Button to view compiled output