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