0
0
Rubyprogramming~20 mins

Why functional patterns complement OOP in Ruby - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Functional OOP Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of combining functional and OOP styles
What is the output of this Ruby code that mixes functional style with OOP?
Ruby
class Calculator
  def initialize(numbers)
    @numbers = numbers
  end

  def double_and_sum
    @numbers.map { |n| n * 2 }.reduce(0, :+)
  end
end

calc = Calculator.new([1, 2, 3])
puts calc.double_and_sum
A6
B9
CError: undefined method `map' for Integer
D12
Attempts:
2 left
💡 Hint
Think about how map and reduce work on arrays inside the class.
🧠 Conceptual
intermediate
1:30remaining
Why use functional patterns in OOP?
Which reason best explains why functional programming patterns complement object-oriented programming?
AThey help manage side effects and make code easier to test.
BThey replace the need for classes and objects entirely.
CThey force all data to be mutable for flexibility.
DThey make code run faster by avoiding objects.
Attempts:
2 left
💡 Hint
Think about how pure functions behave compared to methods with side effects.
🔧 Debug
advanced
2:00remaining
Identify the error mixing functional and OOP styles
What error does this Ruby code produce when mixing functional and OOP styles?
Ruby
class DataProcessor
  def initialize(data)
    @data = data
  end

  def process
    @data.map! { |x| x * 2 }
    @data.reduce(:+)
  end
end

processor = DataProcessor.new(10)
puts processor.process
ANoMethodError: undefined method `map!' for Integer
BTypeError: no implicit conversion of Integer into Array
CSyntaxError: unexpected end-of-input
DNilClass error when calling reduce
Attempts:
2 left
💡 Hint
Check the type of @data when calling map! inside the class.
📝 Syntax
advanced
2:00remaining
Correct functional method syntax in OOP context
Which option correctly defines a method in Ruby that uses a functional style inside a class?
Ruby
class Transformer
  def initialize(values)
    @values = values
  end

  def transform
    # Fill in the blank
  end
end
A
def transform
  @values.reduce { |sum, v| sum + v + 1 }
end
B
def transform
  @values.map { |v| v + 1 }
end
C
def transform
  @values.map! { |v| v + 1 }
end
D
def transform
  @values.each { |v| v + 1 }
end
Attempts:
2 left
💡 Hint
Functional style prefers returning new collections without modifying originals.
🚀 Application
expert
3:00remaining
Predict the final output combining OOP and functional methods
Given this Ruby code combining OOP and functional patterns, what is the final output?
Ruby
class Stats
  def initialize(numbers)
    @numbers = numbers
  end

  def mean
    @numbers.reduce(0.0, :+) / @numbers.size
  end

  def squared_diffs
    @numbers.map { |n| (n - mean) ** 2 }
  end

  def variance
    squared_diffs.reduce(0.0, :+) / @numbers.size
  end
end

stats = Stats.new([2, 4, 4, 4, 5, 5, 7, 9])
puts stats.variance.round(2)
A3.5
B2.0
C4.0
D5.0
Attempts:
2 left
💡 Hint
Calculate mean first, then squared differences, then average those.