What if your code could be as predictable and reliable as a simple math problem?
Why Pure functions concept in Ruby? - Purpose & Use Cases
Imagine you have a recipe book where each recipe changes ingredients randomly every time you cook. You try to follow the recipe, but the results are never the same. This is like writing code where functions change things outside themselves or depend on hidden information.
When functions depend on or change things outside their own work, it becomes hard to predict what will happen. Bugs hide easily, and fixing one problem might cause another. It's like trying to fix a leaking pipe but water keeps flowing from unknown places.
Pure functions always give the same output for the same input and never change anything outside themselves. This makes your code like a reliable recipe: you know exactly what to expect every time you run it. It's easier to test, understand, and fix.
def add_to_list(item, list) list << item return list end
def add_to_list(item, list)
list + [item]
endPure functions let you build programs that are easier to trust, test, and change without unexpected surprises.
Think of a calculator app: pressing 2 + 3 always shows 5, no matter what happened before. Pure functions help make that consistency happen in code.
Pure functions always return the same result for the same input.
They do not change anything outside themselves.
This makes code easier to understand and debug.
Practice
pure function in Ruby?Solution
Step 1: Understand pure function definition
A pure function always returns the same result when given the same input and does not affect anything outside itself.Step 2: Compare options to definition
A function that always returns the same output for the same input and has no side effects. matches this definition exactly. Options A, B, and D involve side effects or changing external state.Final Answer:
A function that always returns the same output for the same input and has no side effects. -> Option BQuick Check:
Pure function = consistent output + no side effects [OK]
- Thinking functions that modify variables are pure
- Confusing input/output consistency with side effects
- Assuming printing output is allowed in pure functions
Solution
Step 1: Check each function for side effects and output consistency
def add(a, b); a + b; end returns sum without changing anything outside or printing. def add(a, b); puts a + b; end prints (side effect). def add(a, b); @sum = a + b; end changes instance variable (side effect). def add(a, b); print a + b; end prints (side effect).Step 2: Identify pure function
Only def add(a, b); a + b; end meets pure function rules: same output for same input, no side effects.Final Answer:
def add(a, b); a + b; end -> Option AQuick Check:
Pure function = no side effects + returns value [OK]
- Confusing printing output with returning value
- Assuming modifying instance variables is allowed
- Assuming print doesn't cause side effects
def multiply(x, y) x * y end result = multiply(3, 4) puts result
Solution
Step 1: Understand the multiply function
The function returns the product of x and y, so multiply(3, 4) returns 12.Step 2: Check the output statement
puts prints the returned value, so it prints 12.Final Answer:
12 -> Option CQuick Check:
multiply(3,4) = 12 [OK]
- Thinking function returns nil if no explicit return
- Confusing addition with multiplication
- Expecting error due to missing return keyword
def increment(n) n += 1 puts n end
Solution
Step 1: Check for side effects
The function prints the value of n, which is a side effect because it affects outside world.Step 2: Verify other aspects
Modifying n locally is allowed; syntax is correct; implicit return is allowed but printing breaks purity.Final Answer:
It prints output, causing a side effect. -> Option DQuick Check:
Pure functions must not print or cause side effects [OK]
- Thinking local variable change breaks purity
- Assuming implicit return is an error
- Ignoring side effects caused by printing
Solution
Step 1: Understand pure function requirements
A pure function returns new data without changing input or external state.Step 2: Analyze each option
def double_list(nums) nums.collect! { |n| n *= 2 } nums end uses collect! which modifies original array (side effect). def double_list(nums) nums.map { |n| n * 2 } end returns a new array with doubled values without changing original. def double_list(nums) nums.map! { |n| n * 2 } end uses map! which modifies original array (side effect). def double_list(nums) nums.clear nums.push(2, 4, 6) end clears and modifies original array (side effect).Final Answer:
def double_list(nums) nums.map { |n| n * 2 } end -> Option AQuick Check:
Pure function = no input change + returns new data [OK]
- Using destructive methods like map! or clear
- Modifying input array instead of returning new one
- Confusing each with map for transformation
Ready to prove it?
Pass the test and this pattern moves unknown
