Ruby: Multiply all elements of an array
This is the textbook case for inject (also called reduce
)
[1, 2, 3, 4, 5].inject(:*)
As suggested below, to avoid a zero,
[1, 2, 3, 4, 5].reject(&:zero?).inject(:*)
How to multiply each number in array by 2 in ruby
You could use map
and over each element in the array check if is Numeric
, and if so, then multiply it by 2, then you can compact your result for nil
values:
p ['a', 2, 2.5].map{|e| e * 2 if e.is_a? Numeric}.compact
# => [4, 5.0]
If you want to leave the elements which won't be applied the *2
operation, then:
p ['a', 2, 2.5].map{|e| e.is_a?(Numeric) ? e * 2 : e}
Also you could use grep
to simplify the checking, and then map your only-numeric values:
p ['a', 2, 2.5].grep(Numeric).map{|e| e*2}
# => [4, 5.0]
I don't know the side effects of doing this, but looks good (of course if the output won't be only Numeric objects):
class Numeric
def duplicate
self * 2
end
end
p ['a', 2, 2.5].grep(Numeric).map(&:duplicate)
Or also:
p ['a', 2, 2.5].grep(Numeric).map(&2.method(:*))
# [4, 5.0]
How can I multiply two arrays in Ruby?
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]
Multiply every element of an n-dimensional array by a number in Ruby
The long-form equivalent of this is:
[ 1, 2, 3, 4, 5 ].collect { |n| n * 2 }
It's not really that complicated. You could always make your multiply_by
method:
class Array
def multiply_by(x)
collect { |n| n * x }
end
end
If you want it to multiply recursively, you'll need to handle that as a special case:
class Array
def multiply_by(x)
collect do |v|
case(v)
when Array
# If this item in the Array is an Array,
# then apply the same method to it.
v.multiply_by(x)
else
v * x
end
end
end
end
Ruby: how to repeat/multiply elements in array?
Try this one
array.flat_map { |item| Array.new(5, item) }
[
[ 0] "A",
[ 1] "A",
[ 2] "A",
[ 3] "A",
[ 4] "A",
[ 5] "B",
[ 6] "B",
[ 7] "B",
[ 8] "B",
[ 9] "B",
[10] "C",
[11] "C",
[12] "C",
[13] "C",
[14] "C"
]
Related Topics
Ruby Method, Proc, and Block Confusion
How to Best Create a Random Float in a Range Between Two Floats
Heroku: Pg::Connectionbad: Could Not Connect to Server: Connection Refused
Have a Parent Class's Method Access the Subclass's Constants
How to Use Fiddle to Pass or Return a Struct to Native Code
Equivalent Ruby .Times in Coffeescript
Solving Dependency Constraints
Did I Install Ruby 1.9.3 Correctly on Rhel
"The Ruby Way" (Mixins and Class Reopening) VS. Dependency Injection
Escaping the .Each { } Iteration Early in Ruby
How to Make a Custom Environment in Rails a Default Environment
Text_Field_With_Auto_Complete Inside Form_For
Invalid Configuration or No Rubies Listed
Is There a Shorthand If (Without Else) Statement in Ruby on Rails
Os X Mavericks Install Rvm Warning