I have a method that tries to parse a query into a a hash.
CONTACT_SEARCH_FIELDS = ['LastUpdate','Name','RecAdd','PhoneNumber','Tag_Group','FirstName','LastName','FamilyName','FamilyHead','ClientStatus','ContactType','ClientSource','TaxId']
CONTACT_SEARCH_OPERANDS = ['=','>','<','!=','Like','BeginsWith','IsEmpty']
def search (query, page = 1)
body = [{}]*query.length
query.each_with_index do |expr, i|
body[i]["Field"] = CONTACT_SEARCH_FIELDS.index expr[0]
body[i]["Operand"] = CONTACT_SEARCH_OPERANDS.index expr[1]
body[i]["Value"] = expr[2]
end
return body
end
The method is called like this
search([["FirstName", "=", "John"], ["LastName", "=", "Smith"]])
The problem is that running this gives a very weird output.
search([["FirstName", "=", "John"], ["LastName", "=", "Smith"]])
=> [{"Operand"=>0, "Value"=>"Smith", "Field"=>6}, {"Operand"=>0, "Value"=>"Smith", "Field"=>6}]
I did some debugging and the problem is that all the hashes in the array are get set on every iteration.
I dont understand what is the reason behind this. I would also welcome any shorter or better versions of this code.