0
0
Rubyprogramming~30 mins

Why testing is central to Ruby culture - See It in Action

Choose your learning style9 modes available
Why Testing is Central to Ruby Culture
📖 Scenario: Imagine you are part of a team building a small Ruby program that manages a list of tasks. To make sure your program works well and keeps working as you add new features, you will write tests. Testing is a big part of Ruby culture because it helps catch mistakes early and makes coding more fun and reliable.
🎯 Goal: You will create a simple Ruby class to hold tasks, set up a test configuration, write a test to check your code, and finally run the test to see the result. This will show why testing is important and how it fits naturally in Ruby programming.
📋 What You'll Learn
Create a Ruby class called TaskList with an empty array called tasks
Add a configuration variable max_tasks set to 5
Write a method add_task that adds a task only if the list has fewer than max_tasks
Write a test using minitest to check that add_task works correctly
Print the test result to show if the test passed or failed
💡 Why This Matters
🌍 Real World
Testing is used in real Ruby projects to make sure code works well before sharing with others or adding new features.
💼 Career
Ruby developers often write tests as part of their daily work to keep code quality high and avoid bugs.
Progress0 / 4 steps
1
Create the TaskList class with tasks array
Create a Ruby class called TaskList with an instance variable @tasks initialized as an empty array inside the initialize method.
Ruby
Need a hint?

Use @tasks = [] inside the initialize method to create an empty list.

2
Add max_tasks configuration variable
Inside the TaskList class, add an instance variable @max_tasks set to 5 inside the initialize method.
Ruby
Need a hint?

Set @max_tasks = 5 inside the initialize method.

3
Write add_task method with limit check
Add a method add_task to the TaskList class that takes a task parameter and adds it to @tasks only if the number of tasks is less than @max_tasks.
Ruby
Need a hint?

Use if @tasks.size < @max_tasks to check before adding the task.

4
Write and run a test for add_task method
Write a test using minitest that creates a TaskList object, adds a task, and uses assert_equal to check that the task was added. Then print the test result.
Ruby
Need a hint?

Use assert_equal(['Buy milk'], list.tasks) inside a test_add_task method.