views:

281

answers:

1

I will make this quick. I'm using Ruby/LDAP to search using my base_dn criteria. I get a result back (of type Entry). I can convert object of type Entry to Hash with to_hash method. The problem is when the result is returned it is multiple objects of type Entry. I want to convert them to hash append them while looping. Look at this:

connection.search(base_dn,scope,filter) do |entry|
        #pp entry.to_hash
        searchResult.merge!(entry.to_hash)
end

You know if i am looping through and I would like to add objects to an array, I can use << and it will added it as it goes through. I read the RDOC, the merge or update method of hash should do similar but to no avail. Could someone give me a hint on how I can convert Entry objects to hash and appended.

The above code gives me the last entry in the search. It is basically overwriting searchResult hash every time rather than merging with the existing hash. Thanks in advance.

+1  A: 

When using Hash#merge! you will need to supply an additional hash that is keyed properly or it will just blend all your results together.

I'll bet what you really need is something akin to:

connection.search(base_dn,scope,filter) do |entry|
  searchResult[entry.id] = entry.to_hash
end

Here entry.id represents a unique identifier that can be used to split out the entry records.

Since a Hash is a key/value store, you can't really "append" to it like you can an array, which is essentially a list of objects.

tadman
Ah... that works perfectly .. thank you!
Nikolas Sakic
Also, thanks for explaining why I was doing wouldn't have worked. I didn't look right to me either. Thanks very much
Nikolas Sakic