I have a ruby method that queries a array through a search. From what is typed in the searchbar it appends that item to another array that shows on the screen. Below is my method:
@churches = ["UGBC", "Oak Ridge Baptist", "Calvary"]
@search_results = []
def search_for(text)
@churches.collect do |church|
if text == church
@search_results << church
end
end
end
When the code is triggered the only object that is returned when searched is the last object in the array: "Calvary". If you search any of the other items it sends back an empty array. I have tried an each statement and a collect statement and nothing will work. How do I get it to append to the empty array with no matter what item I search on?
You are not using the right method. I think Array#select is what you need. Also you might have to do a substring check for the text.
Here is an example
text = 'Ridge'
> churches.select{|c| c.include?(text)}
#=> ["Oak Ridge Baptist"]