10 Ruby Basic Exercises for Advanced with Solutions
Master advanced Ruby skills with our comprehensive list of top 10 exercises. Dive into coding challenges that improve your understanding and proficiency in Ruby, setting a solid foundation for professional-level challenges. Start your journey to Ruby mastery today!
Learning Objectives:
Master advanced Ruby concepts such as metaprogramming, modules, mixins, and performance optimization. Enhance problem-solving abilities with complex challenges that require deep understanding and efficient coding techniques.
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. Generate an infinite Fibonacci sequence using Enumerator::Lazy and retrieve the first `n` Fibonacci numbers.
Required Input:
n = 10
Expected Output:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Code In Ruby
Run Code?
Click Run Button to view compiled output
2. Create a class that dynamically defines getter and setter methods for attributes based on an array of symbols.
Required Input:
attrs = [:name, :age]
obj = Dynamic.new
Expected Output:
Ruby
30
Code In Ruby
Run Code?
Click Run Button to view compiled output
3. Multiply two matrices represented as arrays of arrays.
Required Input:
[[1, 2], [3, 4]] * [[5, 6], [7, 8]]
Expected Output:
[[19, 22], [43, 50]]
Code In Ruby
Run Code?
Click Run Button to view compiled output
4. Implement a class to represent an undirected graph using a hash where keys are nodes and values are arrays of connected nodes.
Required Input:
g = Graph.new; g.add_edge('A', 'B')
Expected Output:
{"A"=>["B", "C"], "B"=>["A"], "C"=>["A"]}
true
false
Code In Ruby
Run Code?
Click Run Button to view compiled output
5. Sort an array of words based on the number of vowels. If two words have the same count, sort alphabetically.
Required Input:
['apple', 'banana', 'cherry', 'date']
Expected Output:
["cherry", "apple", "date", "banana"]
Code In Ruby
Run Code?
Click Run Button to view compiled output
6. Create a module `CustomEnumerable` that implements `map`, `select`, and `reduce` methods without using built-in Enumerable.
Required Input:
arr = [1, 2, 3]; arr.custom_map { |x| x * 2 }
Expected Output:
[2, 4, 6]
Code In Ruby
Run Code?
Click Run Button to view compiled output
7. Check if a given number is a power of two using bitwise operations.
Required Input:
8
Expected Output:
true
Code In Ruby
Run Code?
Click Run Button to view compiled output
8. Demonstrate differences between Proc and Lambda.
Required Input:
Example method calls
Expected Output:
Before Proc
Code In Ruby
Run Code?
Click Run Button to view compiled output
9. Solve the N-Queens problem for a given board size `n`.
Required Input:
n = 4
Expected Output:
. Q . .
. . . Q
Q . . .
. . Q .
. . Q .
Q . . .
. . . Q
. Q . .
Code In Ruby
Run Code?
Click Run Button to view compiled output
10. Extract all valid email addresses from a given string using regex.
Required Input:
'Contact me at [email protected] or [email protected]'
Expected Output:
Code In Ruby
Run Code?
Click Run Button to view compiled output