Say I have an array of 3 strings:
strings = ["/I/love/bananas", "/I/love/blueberries", "/I/love/oranges"]
I want to compare my 3 strings and print out this new string:
new_string = "/I/love"
I don't want to match char by char, only word by word. Do anyone have a smart way to do that?
As a token of good will I have made this ugly code to show what functionality I am looking for:
strings = ["/I/love/bananas", "/I/love/blueberries", "/I/love/oranges"]
benchmark = strings.first.split("/")
new_string = []
strings.each_with_index do |string, i|
unless i == 0
split_string = string.split("/")
split_string.each_with_index do |elm, i|
final_string.push(elm) if elm == benchmark[i] && elm != final_string[i]
end
end
end
final_string = final_string.join("/")
puts final_string # => "/I/love"