Any string contains "xyz" If there is no period (.) Before xyz, it outputs True and If there is a period (.), Let's create a method that outputs False.
xyz_there('abcxyz') → True
xyz_there('abc.xyz') → False
xyz_there('xyz.abc') → True
Use the include? 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
def xyz_there(str)
if str.include?(".xyz")
puts "False"
elsif str.include?("xyz")
puts "True"
else
puts "False"
end
end
Again, there was no commentary, but it was understandable by reading the answers! I couldn't come up with the correct answer because I couldn't master how to use the include? method. I think the way of thinking was good. (I thought the include? Method could only be used for arrays, so I wrote a description like array = ["foo", "bar"].
In addition to arrays, you can also use the following.
animal = "cat"
puts color.include?("c")
#String"cat"In"c"Is included, so true is returned
(1) Arbitrary character string with str as an argument (anything is fine) (2) If ".xyz" is included in the conditional expression, puts "False" is displayed. ③ If "xyz" is included in the subsequent elsif, return true. ④ If both are not included, return with False.
after str.include? (". Xyz") If you don't write "puts" False "", True will be called. Awaken with puts.
Recommended Posts