0
0
Rubyprogramming~15 mins

Include for instance methods in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Include for Instance Methods in Ruby
📖 Scenario: You are building a simple program to manage animals in a zoo. Some animals can make sounds, and you want to share this ability using a module.
🎯 Goal: Create a module with an instance method, include it in a class, and use the method from an instance of that class.
📋 What You'll Learn
Create a module named Sound with an instance method make_sound that returns the string "Roar!".
Create a class named Lion.
Include the Sound module inside the Lion class.
Create an instance of Lion and call the make_sound method.
Print the result of calling make_sound on the Lion instance.
💡 Why This Matters
🌍 Real World
Modules with instance methods let you share common behaviors across different classes, like animals making sounds in a zoo management system.
💼 Career
Understanding how to use modules and include them for instance methods is important for writing clean, reusable Ruby code in many software projects.
Progress0 / 4 steps
1
Create the Sound module with an instance method
Create a module called Sound with an instance method named make_sound that returns the string "Roar!".
Ruby
Need a hint?

Use module Sound to start and define def make_sound inside it.

2
Create the Lion class and include the Sound module
Create a class called Lion and include the Sound module inside it.
Ruby
Need a hint?

Use class Lion and inside it write include Sound.

3
Create a Lion instance and call make_sound
Create a variable called simba and set it to a new instance of Lion. Then call the make_sound method on simba and store the result in a variable called sound.
Ruby
Need a hint?

Use simba = Lion.new and then sound = simba.make_sound.

4
Print the sound made by simba
Print the value of the variable sound.
Ruby
Need a hint?

Use puts sound to display the sound.