multiply array within ruby - Search
About 159,000 results
Open links in new tab
    Kizdar net | Kizdar net | Кыздар Нет
    Upvotes20Top Answeredited Apr 28, 2018 at 19:16
    a.zip(b).map{|x, y| x * y}

    This works because zip combines the two arrays into a single array of two element arrays. i.e.:

    a = [3, 2, 1]
    b = [1, 2, 3]
    a.zip(b)
    #=> [[3, 1], [2, 2], [1, 3]]

    Then you use map to multiply the elements together. This is done by iterating through each two element array and multiplying one element by the other, with map returning the resulting array.

    a.zip(b).map{|x, y| x * y}
    #=> [3, 4, 3]
    Content Under CC-BY-SA license
    Was this helpful?
     
  1. Ruby: Multiply all elements of an array - Stack Overflow

     
  2. Multiply every element of an n-dimensional array by a number in …

  3. Question & Answer
  4. Ruby: Multiply All Elements of an Array - Terra Magnetica

  5. Ruby | Operators - GeeksforGeeks

  6. math - Array Multiplication and Division - Software Engineering …

  7. How To Use Array Methods in Ruby - DigitalOcean

  8. r/ruby on Reddit: How to multiply one array times another array ...

  9. How To Work with Arrays in Ruby - DigitalOcean

    Jan 27, 2023 · Arrays let you store multiple values in a single variable. In Ruby, arrays can contain any data type, including numbers, strings, and other Ruby objects. This can condense and organize your code, making it more readable …

  10. multiply each element in array ruby | Code Ease

  11. Ruby arrays - working with arrays in Ruby - ZetCode

  12. Multiply elements in array - Ruby - Ruby-Forum

  13. Arrays of Arrays (two-dimensional arrays in Ruby) - CodeAhoy

  14. ruby - Multiply each item in an array by each item in another array ...

  15. MULTIPLYING an array ELEMENT in while loop ruby

  16. class Array - Documentation for Ruby 3.2

  17. Related searches for multiply array within ruby

  18. Some results have been removed