I want to search an array for a certain string and(!) its substrings. For example my array is:
array = ["hello", "hell", "goodbye", "he"]
So when I search for "hello" and its substrings (but only from the beginning: "he", "hell", "hello"), it should return
=> ["hello", "hell", "he"]
What I've tried so far: Using a regular expression with the #grep and/or the #include? method like this:
array.grep("hello"[/\w+/])
or
array.select {|i| i.include?("hello"[/\w+/])}
but in both cases it only returns
=> ["hello"]
By the way, if I try array.select{|i| i.include?("he")} it works but like I said I want it the other way around: searching for "hello" and give me all results including the substrings from the beginning.
eis a substring ofhellosogoodbyeshould be in result. Or did you mean substrings only from beginning of the string?h,he,hel...hello?