__ If the array contains a specific character string, I want to cut it out and perform another process. __
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
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"
else
puts "False"
end
end
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 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