Bird
0
0

Which approach best combines functional patterns with OOP?

hard📝 Application Q15 of 15
Ruby - Functional Patterns in Ruby
You want to create a Ruby class that stores a list of numbers and provides a method to return a new list with each number doubled, without changing the original list. Which approach best combines functional patterns with OOP?
AModify the original list inside the method by doubling each element in place.
BDefine a method that returns a new array using <code>map</code> without modifying the original list.
CStore the doubled values in an instance variable and return it directly.
DUse a global variable to hold the doubled list and update it inside the method.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirement

    The method should not change the original list but return a new list with doubled values.
  2. Step 2: Evaluate options for functional and OOP style

    Using map returns a new array without side effects, fitting functional style. Modifying original data or using global variables breaks this principle.
  3. Final Answer:

    Define a method that returns a new array using map without modifying the original list. -> Option B
  4. Quick Check:

    Use map for new array, avoid side effects [OK]
Quick Trick: Use map to transform arrays without changing originals [OK]
Common Mistakes:
  • Modifying original array instead of returning new
  • Using global variables for data storage
  • Storing results in instance variables unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes