Monday, 17 June 2019

How to Find the sum of total elements in an array using ruby?

a= [1,2,3,4,54,656,76,43,560]

Solution 1:

a.inject(&:+)

output: =>1399

Solution 2:

b = 0
a.each do |e|
  b+=e
end
b

output: =>1399

No comments:

Post a Comment

Interactor in Rails

What is interactor? Interactor provides a common interface for performing complex user interactions An interactor is a simple, sin...