0
0
Rubyprogramming~30 mins

Custom modules as mixins in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Custom modules as mixins
📖 Scenario: Imagine you are building a simple program to manage different types of vehicles. Some vehicles can fly, and some can float on water. You want to add these abilities to your vehicle classes without repeating code.
🎯 Goal: You will create custom modules as mixins to add flying and floating abilities to vehicle classes. Then, you will use these modules in specific vehicle classes and display their abilities.
📋 What You'll Learn
Create a module called Flyable with a method fly that returns the string "I can fly!"
Create a module called Floatable with a method float that returns the string "I can float!"
Create a class Car without any special abilities
Create a class Boat that includes the Floatable module
Create a class Plane that includes the Flyable module
Create an instance of each class: car, boat, and plane
Print the abilities of boat and plane by calling their respective methods
💡 Why This Matters
🌍 Real World
Modules as mixins help you add shared abilities to different classes without repeating code. This is useful in many programs where objects share behaviors.
💼 Career
Understanding modules and mixins is important for Ruby developers to write clean, reusable, and maintainable code.
Progress0 / 4 steps
1
Create the Flyable module
Create a module called Flyable with a method fly that returns the string "I can fly!".
Ruby
Need a hint?

Use module Flyable to start and define a method fly inside it.

2
Create the Floatable module
Create a module called Floatable with a method float that returns the string "I can float!".
Ruby
Need a hint?

Define another module similar to Flyable but with a method float.

3
Create vehicle classes and include modules
Create a class Car with no modules included. Create a class Boat that includes the Floatable module. Create a class Plane that includes the Flyable module.
Ruby
Need a hint?

Use include inside the classes Boat and Plane to add the modules.

4
Create instances and print abilities
Create instances called car, boat, and plane from their classes. Print the result of calling float on boat and fly on plane.
Ruby
Need a hint?

Create the instances with ClassName.new and use puts to print the abilities.