Examine the elements in the array using the [Ruby] includes? Method

__ If the array contains a specific character string, I want to cut it out and perform another process. __

includes? method

The include? method is a method that determines whether the specified element is included in the array.

array = ["foo", "bar"]
puts array.include?("bar")
#=> true
puts array.include?("hoge")
#=> false

Problem example

Create a method that returns "true" if a specific character is included in the array, and "false" if it is not.
Here, the specific character is a number of 1,2,3. I will write the code.

def array123(nums)
  if nums.include?(1) && nums.include?(2) && nums.include?(3)
    puts "True"
    puts "False"

This time we will call the method of array123, setnums as a formal argument, and we are ready to receive the actual argument.

def array123(nums)

If each of 1, 2 and 3 is included using if conditional branching, perform the following processing. Logical operator`&&`(Meaning of)`||`(Or meaning) etc. are used.
if nums.include?(1) && nums.include?(2) && nums.include?(3)

If there is a process you want to do when true, describe it between if ~ else, If there is a process you want to make when false, describe it between else ~ end.
■ Reference reference

Recommended Posts

Examine the elements in the array using the [Ruby] includes? Method
[Ruby] Count an even number in an array using the even? Method
[Ruby] Searching for elements in an array using binary search
Arbitrary search method in an array using binary search
[Ruby] Exclude duplicate elements with the uniq method.
[Ruby] Cut out a string using the slice method
[Ruby] The role of subscripts in learning elements in arrays
Calculate the difference between numbers in a Ruby array
Multiplication in a Ruby array
[Ruby] Get array elements alternately.
Try using gRPC in Ruby
How to retrieve the hash value in an array in Ruby
[Ruby] Get in the habit of using the dup method when making a copy of a string variable
Sorting hashes in a Ruby array
Java comparison using the compareTo () method
[Ruby] Extracting double hash in array
[Ruby] Extracting elements with slice method
String output method memo in Ruby
Hanachan in Ruby (non-destructive array manipulation)
[Rails 6] destroy using the resources method
[Ruby] Search problem using index method
[Ruby] undefined method `dark?'occurs in rqr_code
Call the super method in Java
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
Iterative processing of Ruby using each method (find the sum from 1 to 10)
[Ruby] Let's examine the inheritance tree while watching the flow of method search
Extract elements of array / extract in reverse order-java
Examine the memory usage of Java elements
Try using the Stream API in Java
Map without using an array in java
Implement the algorithm in Ruby: Day 1 -Euclidean algorithm-
[Ruby] From the basics to the inject method
Implemented "Floyd Cycle Detection Method" in Ruby
Organized memo in the head (Java --Array)
Compare the elements of an array (Java)
What is the main method in Java?
[Ruby] Get unique elements from an array
Get the error message using the any? method
[Swift] How to get the number of elements in an array (super basic)
[Ruby on Rails] Solving the addiction when setting crontab using whenever in EC2
[Ruby] Learn how to use odd? Even? And count the even and odd numbers in the array!
Call a method of the parent class by explicitly specifying the name in Ruby
[Ruby] How to calculate the total amount using the initialize method and class variables
[Order method] Set the order of data in Rails
[Java] Handling of JavaBeans in the method chain
[Ruby] Calculation by extracting elements from an array
ChatWork4j for using the ChatWork API in Java
The ruby version is managed in the .rbenv / version file
Implement the algorithm in Ruby: Day 3 -Binary search-
Implement the algorithm in Ruby: Day 4-Linear search-
Display "Hello World" in the browser using Java
Display "Hello World" in the browser using Java
Try using the COTOHA API parsing in Java
How to build the simplest blockchain in Ruby
Finding pi with the Monte Carlo method? (Ruby)
About regular expressions used in ruby sub method
Implement the algorithm in Ruby: Day 2 -Bubble sort-
I want to get the value in Ruby
[Ruby basics] How to use the slice method
The presence or absence of! In Ruby method names does not mean destructive / non-destructive
[Ruby On Rails] In the nested state, the description in parentheses written after Prefix in the link_to method