Functional patterns help make your code simpler and easier to test when you use them with object-oriented programming (OOP).
Why functional patterns complement OOP in Ruby
# Example of a functional method in Ruby def add_one(number) number + 1 end # Example of an OOP class class Counter def initialize @count = 0 end def increment @count += 1 end def value @count end end
Functional methods usually take inputs and return outputs without changing anything else.
OOP classes bundle data and actions together to model real-world things.
def square(x) x * x end
class Person def initialize(name) @name = name end def greet "Hello, #{@name}!" end end
def greet_person(person) "Hello, #{person}!" end
This program shows how a functional method double can be used inside an OOP class Calculator to keep code simple and reusable.
def double(number) number * 2 end class Calculator def initialize(value) @value = value end def double_value double(@value) # Using functional method inside OOP end end calc = Calculator.new(5) puts calc.double_value
Functional patterns help keep methods pure, meaning they don't change outside data.
OOP helps organize code around objects, but mixing in functional methods can reduce complexity.
Using both styles together can make your programs easier to read and maintain.
Functional patterns focus on small, reusable methods without side effects.
OOP organizes code around objects with data and behavior.
Combining both helps write clearer and more reliable Ruby programs.
Practice
Solution
Step 1: Understand functional patterns in Ruby
Functional patterns emphasize small, reusable methods that avoid changing data outside their scope (no side effects).Step 2: Understand OOP's role
OOP organizes code around objects that combine data and behavior, making programs easier to manage.Final Answer:
Functional patterns create small, reusable methods without side effects, improving code clarity. -> Option DQuick Check:
Functional patterns improve clarity by avoiding side effects [OK]
- Thinking functional replaces OOP completely
- Believing OOP ignores methods
- Assuming functional always makes code faster
Solution
Step 1: Identify pure function characteristics
A pure function returns a value based only on input and does not change external variables or produce side effects.Step 2: Check each option
def double(x); x * 2; end returns x * 2 without changing anything else. def double(x); @value = x * 2; end changes an instance variable (@value), so it has side effects. def double(x); puts x * 2; end prints output (side effect). def double(x); x *= 2; nil; end modifies x locally but returns nil, so it doesn't return the doubled value.Final Answer:
def double(x); x * 2; end -> Option AQuick Check:
Pure function returns value only [OK]
- Using instance variables inside pure functions
- Printing inside functions instead of returning
- Not returning the computed value explicitly
class Calculator
def initialize(value)
@value = value
end
def double
@value * 2
end
def apply(func)
func.call(@value)
end
end
calc = Calculator.new(5)
puts calc.double
puts calc.apply(->(x) { x + 3 })Solution
Step 1: Understand the Calculator class methods
The double method returns @value * 2, so for 5 it returns 10. The apply method calls the passed function with @value.Step 2: Evaluate the output of puts statements
calc.double prints 10. calc.apply calls the lambda adding 3 to 5, so prints 8.Final Answer:
10 8 -> Option AQuick Check:
double = 10, apply adds 3 = 8 [OK]
- Confusing lambda call syntax
- Expecting apply to modify @value
- Mixing up output values
class Counter
def initialize
@count = 0
end
def increment
@count += 1
end
def apply(func)
func(@count)
end
end
counter = Counter.new
counter.increment
puts counter.apply(->(x) { x * 2 })Solution
Step 1: Check how func is used in apply method
In Ruby, to call a lambda or Proc, you must use .call, not func().Step 2: Identify the error
func(@count) will cause a NoMethodError because func is a Proc object and needs .call.Final Answer:
func should be called with .call, not func() -> Option CQuick Check:
Call lambdas with .call [OK]
- Calling lambda like a normal method
- Forgetting to initialize instance variables
- Incorrect lambda syntax
Solution
Step 1: Understand the requirement
The method should not change the original list but return a new list with doubled values.Step 2: Evaluate options for functional and OOP style
Usingmapreturns a new array without side effects, fitting functional style. Modifying original data or using global variables breaks this principle.Final Answer:
Define a method that returns a new array usingmapwithout modifying the original list. -> Option BQuick Check:
Use map for new array, avoid side effects [OK]
- Modifying original array instead of returning new
- Using global variables for data storage
- Storing results in instance variables unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
