Quiz: Ruby Internals & Object Model
Test your understanding of Ruby's object model, method lookup chain, eigenclasses, garbage collection, and open classes. This comprehensive quiz covers advanced Ruby internals concepts.
Quiz Instructions
How to Take This Quiz
- Read each question carefully - questions cover object model, method lookup, garbage collection, and open classes
- Think about Ruby's internals - consider how Ruby manages objects, memory, and method resolution
- Use the interactive code runner - test your understanding with real Ruby code
- Take your time - these are advanced concepts that require deep understanding
1
Question 1: Everything is an Object
In Ruby, "everything is an object." Which of the following statements about Ruby's object model are true? (Select all that apply)
💡 Hint: Try using .class, .methods, and .method(:name) to explore Ruby's object model.
2
Question 2: Method Lookup Chain
Given this Ruby code, what will be the method lookup chain when calling obj.greet?
💡 Hint: Use Child.ancestors to see the actual method lookup chain. Remember that included modules are inserted above the class that includes them.
3
Question 3: Eigenclasses & Singleton Methods
What happens when you define a singleton method on a specific object instance?
💡 Hint: Test with str1.singleton_class and str2.respond_to?(:shout) to understand singleton methods.
4
Question 4: Garbage Collection
Which statements about Ruby's mark-and-sweep garbage collection are correct? (Select all that apply)
💡 Hint: Try creating circular references and see if they get collected. Use GC.stat to explore GC metrics.
5
Question 5: Safe Monkey Patching
What is the safest way to add a method to an existing class without causing conflicts?
💡 Hint: Consider safety, scope, and potential conflicts with other code. Multiple approaches can be "safe" depending on the context.
Test Your Knowledge - Interactive Practice
Practice Time: Use the code runner below to experiment with Ruby internals concepts. Try the examples from the quiz questions and explore deeper!
Interactive Code Runner
Ruby Code Editor
Check Your Answers
Question 1 Answer:
Correct answers: Numbers like 42 are objects with methods ✓, Classes are objects and instances of Class ✓, nil/true/false are objects with their own classes ✓.
Incorrect: Methods ARE objects too - you can get them with .method(:name).
Question 2 Answer:
Correct: Child → Parent → Greeting → Object → BasicObject. When a module is included, it's inserted into the lookup chain above the class that includes it.
Question 3 Answer:
Correct: Ruby creates an eigenclass (singleton class) for str1 and adds the method there. This allows individual objects to have unique methods without affecting other instances.
Question 4 Answer:
Correct answers: Mark phase identifies reachable objects ✓, Ruby uses generational GC ✓, GC.stat monitors performance ✓. Incorrect: Circular references do NOT prevent collection - Ruby's GC handles them correctly.
Question 5 Answer:
Multiple correct approaches: Checking if method exists first is safe for global patches. Refinements provide scoped monkey patching. Creating new classes avoids monkey patching entirely. The best choice depends on your specific needs and context.
Quiz Complete - Key Takeaways
Ruby Internals Mastery
- Everything is truly an object: Numbers, classes, methods, and even nil have methods and belong to classes
- Method lookup follows a predictable chain: eigenclass → class → included modules → superclass → Object → BasicObject
- Eigenclasses enable object-specific behavior: Each object can have unique methods via singleton classes
- Garbage collection is sophisticated: Mark-and-sweep with generational collection handles circular references correctly
- Open classes require responsibility: Check for existing methods, use namespaces, or consider refinements for safety