Write me a method in ruby that checks if a substring is contained with a parent string
def contains_substring(parent, child)
parent.include?(child)
end
Is there a better way?
Yes, you can use the index
method to find the index of the first occurrence of the given substring within the parent string and then check if it's equal to -1
. Here's an example using your provided code:
ruby
def contains_substring2(parent, child)
!parent.index(child).nil?
end