0
0
Rubyprogramming~30 mins

Namespacing with modules in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Namespacing with modules
📖 Scenario: Imagine you are organizing a small library system. You want to keep book-related and author-related code separate but still inside the same program. This helps avoid confusion when different parts have similar names.
🎯 Goal: You will create two modules named Books and Authors. Each module will have a class with the same name Info but with different details. Then you will print information from both classes to see how modules keep them separate.
📋 What You'll Learn
Create a module called Books with a class Info inside it
Create a module called Authors with a class Info inside it
Each Info class should have a method details that returns a string
Use the full namespaced class names to create objects and call details
Print the results from both Books::Info and Authors::Info
💡 Why This Matters
🌍 Real World
Namespacing with modules is useful when building large programs or libraries where different parts might have classes or methods with the same names.
💼 Career
Understanding modules and namespacing is important for writing clean, maintainable Ruby code and working on team projects or open-source libraries.
Progress0 / 4 steps
1
Create the Books module with Info class
Create a module called Books. Inside it, define a class called Info. Add a method details that returns the string "Book: Ruby Programming".
Ruby
Need a hint?

Use module Books to start the module. Inside it, define class Info and a method details that returns the exact string.

2
Create the Authors module with Info class
Add a new module called Authors. Inside it, define a class called Info. Add a method details that returns the string "Author: Jane Doe".
Ruby
Need a hint?

Similar to step 1, create module Authors with class Info and a details method returning the exact string.

3
Create objects using namespaced classes
Create a variable book_info and assign it an object of Books::Info.new. Create another variable author_info and assign it an object of Authors::Info.new.
Ruby
Need a hint?

Use the full namespaced class names with :: to create new objects and assign them to the variables.

4
Print details from both objects
Use puts to print the result of calling details on book_info and then on author_info.
Ruby
Need a hint?

Use puts book_info.details and puts author_info.details to print the strings returned by the methods.