🌿 What?

  • 🌱 each_with_object là method viết gọn cho TH chúng ta cần khởi tạo biến để lưu giá trị sau từng lần lặp. Tương tự như reduce().
numbers = [1, 2, 3, 4, 5]
 
def specify_array(array)
  array.each_with_object([]) { |n, arr| arr << n if n > 3 }
end
 
specify_array(numbers)
=> [4, 5]

🌿 Compare with reduce()

  • 🌱 Khác nhau về thứ tự tham số.
numbers = [1, 2, 3, 4, 5]
 
# initial object is first arg, second arg is array's element
sum_by_reduce = numbers.reduce(0) { |sum, num| sum + num }
 
# opposite to reduce() method
sum_by_each_with_object = numbers.each_with_object(0) { |num, sum| sum += num }
  • 🌱 Thêm nữa là reduce sẽ trả về đối giá trị tích lũy còn each_with_object trả về object khởi tạo. Để ý syntax của 2 ví dụ trên, each_with_object phải sử dụng += còn reduce thì không.

🌿 Refer