How to calculate the Sum of all Values in a Ruby Array -


इस सवाल का पहले से ही एक उत्तर है: < / P>

  • 18 उत्तर

मेरे पास एक सरणी है :

a = [1,2,5,8]

मैं एक साथ जोड़ (या गुणा) सभी तत्वों के मूल्य की गणना करना चाहता हूँ।

<पूर्व> a.inject {| sum, x | राशि + x}

या थोड़ा छोटा और तेज़:

  a.inject (: +)  

के लिए गुणन या जो कुछ भी, बस साइन इन करें: a.inject (: *)


Comments

Popular posts from this blog

python - rpy2 import is not working -

c++ - How to load a bitmap from file into the samples buffer -

javascript - How to get MySQL query result returned using $.ajax -