سؤال

I have an array of as given below.

For example:

array = [20%,*,30%, 10%]

I want to add the values except '*' using ruby, Please give me some clue for that

هل كانت مفيدة؟

المحلول

First below is not valid array.

array = [20%,*,30%, 10%]

If your array is

array = [ 20, 1, 1, 1, 30, 10 ]

If you want to sum array excluding 1 then use

array.reject { |value| value == 1 }.sum
=> 60

نصائح أخرى

array.except{ |a| a == * }.reduce(:+)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top