0
0
Rubyprogramming~15 mins

Pure functions concept in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Pure Functions Concept
📖 Scenario: Imagine you are building a simple calculator that adds numbers without changing anything outside the calculator. This means the calculator should always give the same answer for the same numbers and never change any other data.
🎯 Goal: You will create a pure function in Ruby that adds two numbers and returns the result without changing any outside data.
📋 What You'll Learn
Create a method called add_numbers that takes two parameters: a and b.
Inside the method, return the sum of a and b.
Create two variables called num1 and num2 with values 5 and 7 respectively.
Call the add_numbers method with num1 and num2 as arguments and store the result in a variable called result.
Print the value of result.
💡 Why This Matters
🌍 Real World
Pure functions are used in many programs to make sure calculations are reliable and do not cause unexpected changes.
💼 Career
Understanding pure functions is important for writing clean, testable, and maintainable code in software development jobs.
Progress0 / 4 steps
1
Create two number variables
Create two variables called num1 and num2 and set them to 5 and 7 respectively.
Ruby
Need a hint?

Use = to assign values to variables.

2
Define a pure function to add numbers
Define a method called add_numbers that takes two parameters a and b and returns their sum.
Ruby
Need a hint?

Use def to start a method and end to finish it. Return the sum by writing a + b.

3
Call the pure function and store the result
Call the method add_numbers with num1 and num2 as arguments and store the result in a variable called result.
Ruby
Need a hint?

Call the method by writing its name and passing variables inside parentheses.

4
Print the result
Print the value of the variable result using puts.
Ruby
Need a hint?

Use puts result to show the output.