Multiplication in a Ruby array

Multiplication in an array (standard input)

Three integers are given separated by spaces. Output the value obtained by multiplying three integers.

For example

4 10 5

In this case 4 x 10 x 5

200

Please output.

How to do. 1

ruby.rb


numbers = gets.split(' ').map(&:to_i)
a = 1
numbers.each do |number|
    a = a * number
end
puts a

Commentary

numbers = gets.split(' ').map(&:to_i)

・ Call the input element with the get method -Split the character string separated by commas with the split method ・ Extract elements one by one with the map method and convert them to integers with to_i

a = 1
numbers.each do |number|
    a = a * number
end

・ Substitute 1 for a at ʻa = 1` (initial value) ・ Substitute the elements of numbers into the number variable and repeat the process below.

How to do. 2

How to inject method

numbers = gets.split(' ').map(&:to_i).inject(:*)

puts numbers

inject method (method to calculate the sum of arrays)

inject is a method that iterates like each and map.

The feature is that iterative calculations are performed using blocks.

Array object.inject {|initial value,element|Block processing}

It is described as.

The elements of the block are added by the array in the order of repetition, and the calculation is performed by processing the block.

Specify an operator using a symbol for inject

You can write the inject more stylishly by passing the operator as a symbol to the inject.

For example

It can be used in various situations such as *.

array = 1..6
p array.inject(:+) #Add all the elements of the array
p array.inject(3,:+) #Add all the elements of the array to the initial value of 3
p array.inject(:*) #Multiply all the elements of the array
p array.inject(3,:*) #Multiply all the elements of the array by the initial value of 3
p array.inject(100,:-) #Subtract the total value of array from 100

[Execution result]

21
24
720
2160
79

Summary

The method using the inject method pointed out in the comment is smarter and easier to calculate! Thank you for your comment.

Recommended Posts

Multiplication in a Ruby array
Sorting hashes in a Ruby array
[Ruby] Extracting a two-dimensional array
[Ruby] Extracting double hash in array
Implement a gRPC client in Ruby
Hanachan in Ruby (non-destructive array manipulation)
What is a Ruby 2D array?
How to change a string in an array to a number in Ruby
I tried a calendar problem in Ruby
When seeking multiple in a Java array
About array multiplication
[Ruby] I want to put an array in a variable. I want to convert to an array
Class in Ruby
Ruby two-dimensional array
Heavy in Ruby! ??
Ruby array manipulation
Escape processing when creating a URL in Ruby
I made a Ruby extension library in C
(Ruby on Rails6) Creating data in a table
Create a native extension of Ruby in Rust
About eval in Ruby
Output triangle in Ruby
Variable type in ruby
Fast popcount in Ruby
Multidimensional array in Swift
Count the number of occurrences of a string in Ruby
[Ruby / Rails] Set a unique (unique) value in the class
What impressed me as a beginner in writing Ruby
How to launch another command in a Ruby program
How to convert a file to a byte array in Java
[Programming complete] §5 Create a review management app in Ruby
I got stuck in a clone of a two-dimensional array
Use Coveralls with GitHub Actions in a Ruby repository
Ruby Learning # 2 Drawing a Shape
ABC177 --solving E in Ruby
[Ruby] Count an even number in an array using the even? Method
[Ruby] Searching for elements in an array using binary search
Implemented XPath 1.0 parser in Ruby
Difficulties in building a Ruby on Rails environment (Windows 10) (SQLite3)
What is a Ruby module?
Read design patterns in Ruby
How to divide a two-dimensional array into four with ruby
Determine that the value is a multiple of 〇 in Ruby
Convert a Java byte array to a string in hexadecimal notation
Creating a calendar using Ruby
Update Ruby in Unicorn environment
Integer unified into Integer in Ruby 2.4
[Ruby] Exception handling in functions
Read WAV data as a byte array in Android Java
Ruby Learning # 11 Building a Calculator
Use ruby variables in javascript.
How to retrieve the hash value in an array in Ruby
I searched for a web framework with Gem in Ruby
About regular expressions in Ruby
Learning Ruby with AtCoder 13 How to make a two-dimensional array
Find a subset in Java
Create a fortune using Ruby
How to display a graph in Ruby on Rails (LazyHighChart)
Ruby Learning # 32 Building a Quiz
How to add the same Indexes in a nested array
I want to create a Parquet file even in Ruby