To check if a string contains a given substring in Ruby, you can use the 'include?' method.
In this example,
def contains_substring(str, substr)
str.include?(substr)
end
str = 'Hello World'
substr = 'World'
contains = contains_substring(str, substr)
puts "String contains substring '#{substr}': #{contains}"
String contains substring 'World': true
In this tutorial, we learned How to Check if a String contains given Substring in Ruby language with well detailed examples.