Exchanges

Write me a method in ruby that checks if a substring is contained with a parent string

You said

Write me a method in ruby that checks if a substring is contained with a parent string

AI said
def contains_substring(parent, child)
  parent.include?(child)
end
You said

Is there a better way?

AI said

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