0
0
Rubyprogramming~30 mins

Why functional patterns complement OOP in Ruby - See It in Action

Choose your learning style9 modes available
Why functional patterns complement OOP
📖 Scenario: Imagine you are building a simple program to manage a list of tasks. You want to use Object-Oriented Programming (OOP) to organize your tasks as objects. But you also want to use functional programming patterns to process and transform your tasks easily and clearly.
🎯 Goal: You will create a list of tasks as objects, then use functional patterns like map and select to work with these tasks. This shows how functional programming complements OOP by making data processing simple and readable.
📋 What You'll Learn
Create a list of task objects with specific attributes
Add a configuration variable to filter tasks by priority
Use functional methods like map and select to process tasks
Print the filtered and transformed list of task names
💡 Why This Matters
🌍 Real World
Combining OOP and functional programming helps build clear and maintainable programs, like task managers or inventory systems.
💼 Career
Many software jobs require using OOP for structure and functional patterns for data processing, so this skill is very useful.
Progress0 / 4 steps
1
Create a list of task objects
Create a class called Task with attributes name and priority. Then create an array called tasks with these three tasks as Task objects: 'Wash dishes' with priority 2, 'Do homework' with priority 1, and 'Read book' with priority 3.
Ruby
Need a hint?

Define a class with attr_reader for attributes. Then create an array with new objects.

2
Add a priority filter variable
Create a variable called priority_threshold and set it to 2. This will be used to select tasks with priority less than or equal to this value.
Ruby
Need a hint?

Just create a variable and assign the number 2.

3
Filter and transform tasks using functional methods
Use select on tasks to get only tasks with priority less than or equal to priority_threshold. Then use map to get an array of task names from the filtered tasks. Store this result in a variable called filtered_task_names.
Ruby
Need a hint?

Chain select and map to filter and then get names.

4
Print the filtered task names
Write a puts statement to print the filtered_task_names array.
Ruby
Need a hint?

Use puts to print the array of names.