[Ruby] Obtaining even values ​​using the even? Method

I am reviewing the drill to strengthen my logical thinking. As a beginner, I would appreciate it if you could let me know if you have any questions.

problem

From the values ​​in the array, let's create a method that counts and outputs an even number using the even? method.

Output example:

count_evens([2, 1, 2, 3, 4]) → 3 count_evens([2, 2, 0]) → 3 count_evens([1, 3, 5]) → 0

Model answer

def count_evens(nums)     
  count = 0               #③
    nums.each do |num|    #①
      if num.even?        #②
        count += 1
      end
    end
  puts count              #④
end

Commentary

① each method

Fetch the values ​​in the array one by one. nums is taken out one by one with each and assigned to num.

② if statement

If num is an even number, it is counted by count + = 1. If it is an even number, the even? method determines.

③ Preparation of empty variables

In order to count in (2), it is necessary to prepare a variable, so set count = 0. One thing to note here is the location. If you put it in an if statement or each statement, count = 0 will be executed every time it is processed, so be sure to put it outside the each statement.

④ Output with puts

Like count = 0, put puts outside the each statement so that it is output only once at the end. The processing of each statement is completed, and the final number assigned to count becomes an even number.

Recommended Posts

[Ruby] Obtaining even values ​​using the even? Method
[Ruby] Count an even number in an array using the even? Method
[Ruby] Cut out a string using the slice method
Examine the elements in the array using the [Ruby] includes? Method
Java comparison using the compareTo () method
[Rails 6] destroy using the resources method
[Ruby] Search problem using index method
[Ruby] From the basics to the inject method
Get the error message using the any? method
Iterative processing of Ruby using each method (find the sum from 1 to 10)
Ruby to_s method
[Ruby] slice method
[Ruby] end_with? method
[Ruby] Method memorandum
About the method
[Ruby] How to calculate the total amount using the initialize method and class variables
[Ruby] Exclude duplicate elements with the uniq method.
[Ruby] initialize method
Ruby build method
Ruby accessor method
Finding pi with the Monte Carlo method? (Ruby)
ruby map method
[Ruby] Setting values ​​and memorandum for the table
[Ruby basics] How to use the slice method
[Ruby] Implement error handling / iteration processing using while method
[Ruby] Method to easily get the receiver type .class
[Ruby] Misunderstanding that I was using the module [Beginner]
I want to call the main method using reflection
Are you using the default method of the interface properly?
Try using the query attribute of Ruby on Rails
Output about the method # 2
Ruby Learning # 30 Initialize Method
[Ruby] Get in the habit of using the dup method when making a copy of a string variable
abbreviation for ruby method
Ruby Learning # 24 Exponent Method
Ruby Thread # [] = method notes
About the length method
definition of ruby method
About the authenticate method.
Training using each_with_index method
About the map method
About the ancestors method
[Ruby] Method definition summary
About the [ruby] operator
About the to_s method.
Try using each_with_index method
[Rails] I tried using the button_to method for the first time
[Ruby] Questions and verification about the number of method arguments
[Ruby] Creating code using the concept of classes and instances
[Ruby] Display today's day of the week using Date class
Summary of values returned by the Spliterator characteristics method #java