You are printing the "account" itself (and not the product result) because the terms are interpolated within a string:
puts "#{index} * #{value}"
It will print, for each iteration, the index value, followed by an asterisk between spaces, with the value at the end. You are not, in fact, evaluating the operands. In the above section, the *
is not the multiplication operator, it is only a part of the string.
In this case as you only want the result, it makes no sense to use interpolation. Just print the result of the operation. So:
array = [2, 4, 6, 8]
array.each_with_index do |value, index|
puts index * value
end
Above, the *
is in fact playing the role of multiplier operator and therefore the product results will be correctly evaluated and printed.
Interpolation, therefore, would only make sense if you wanted to show each "armed account", followed by its respective result:
array = [2, 4, 6, 8]
array.each_with_index do |value, index|
puts "#{index} * #{value} = #{index * value}"
end
See the two examples working on Repl.it.
Important you
EDITAR
this question, explaining it clearly, objectively and directly, emphasizing the difficulty found. Furthermore, provide us with a Minimum, complete and verifiable example problem, along with its attempt to resolve. What’s more, I suggest reading the Stack Overflow Survival Guide in English to better understand the functioning of the platform, avoiding further frustrations.– Solkarped