The end user of our rails application can pass in a negative term in the url parameter. That's a term with a minus in front of it. An example is the following: localhost:80/search?q=Arnold+Schwarz+-applesauce+-cantaloop
I'm assuming in the params hash the value of q will be:
"Arnold Schwarz -applesauce -cantaloop"
I want to be able to populate an array in ruby that extracts all negative terms from the string. Here is my code below which does not seem to work correctly. It removes the -applesauce from the query_string
and puts that into ret_hash["excluded_terms"], but does not remove the -cantaloop
.
query_string = "Arnold Schwarz -applesauce -cantaloop"
exclude_terms = Array.new
def compose_valid_query_string(query_string)
split_string = query_string.split
ret_hash = {}
split_string.each do |term|
if(term.start_with?("-"))
deleted_term = split_string.delete(term)
( ret_hash["excluded_terms"] ||= [] ) << deleted_term
end
end
ret_hash["query_string"] = split_string
return ret_hash
end
Aucun commentaire:
Enregistrer un commentaire