If I would create a new array out of a preexisting one and modify the content using math operators, what would be a good way to do so? When I try with the code below, I receive an error about undefined methods.
ary1 = [1, 2, 3, 4, 5]
ary2 = ary1.each { |i|
ary1[i] = ary1[i] * 10
}
p ary1
p ary2
I assume that math operators are not included in the Array class.
"I assume that math operators are not included in the Array class.". Array has no need for math operators because it's a container of other objects. The objects themselves might have math operators. Array does have "set" operators, but those have a different purpose. Typically we usemapand its aliases, oreach_with_objectorinjectto manipulate/coerce the contents of an array.rior checking Array class on ruby-doc.org as seen here. The problem was that you were trying to use * on nil not on Array.