how to 3 × 1 10 100 such that the result is 3 30 300? in ruby

To achieve the desired result, you can multiply each element of the array [1, 10, 100] by 3 using the map method in Ruby. Here's how you can do it:

main.rb
array = [1, 10, 100]
result = array.map { |num| num * 3 }
puts result.inspect
78 chars
4 lines

The map method applies the given block of code to each element of the array and returns a new array with the results. In this case, we multiply each element by 3, resulting in [3, 30, 300]. The inspect method is used to display the resulting array.

Output:

main.rb
[3, 30, 300]
13 chars
2 lines

This code will give you the desired result.

related categories

gistlibby LogSnag