hash key value pairs

Hi

It seems to me that you cant append to hash. I am trying to find a way to dynamically create key value pairs.

Example

   some_hash = {"name" => "tom", "uid"="100", "mail" = "tom.dot.com" }

Now I have function which loops thru 100 users and I would need to append them to end of that hash ie.

some_hash should start to look like this:

some_hash = {"name" => "tom", "uid"="100", "mail" = "tom.dot.com", "name" => "diana", "uid="101", "mail" => "diana.dot.com", .... and so }

I tried has merge but since keys are same for all entries ie. (name, uid, mail) it only replaced current value and I ended up having a hash with only last value on that loop.

If this is not possible whats the way to dynamically create such key, value pairs so I can loop then thru and check if key value is uid (or email or name) and then do whatever ...

Hi

It seems to me that you cant append to hash. I am trying to find a way to dynamically create key value pairs.

Example

  some_hash = {"name" => "tom", "uid"="100", "mail" = "tom.dot.com" }

Now I have function which loops thru 100 users and I would need to append them to end of that hash ie.

some_hash should start to look like this:

some_hash = {"name" => "tom", "uid"="100", "mail" = "tom.dot.com", "name" => "diana", "uid="101", "mail" => "diana.dot.com", .... and so }

I tried has merge but since keys are same for all entries ie. (name, uid, mail) it only replaced current value and I ended up having a hash with only last value on that loop.

that's pretty much the definition hash: a key maps to a unique value.
Do you in fact just want an array of hashes?

Fred

yes that would do but I cant seem to find any examples how to do this

yes that would do but I cant seem to find any examples how to do this

Well it's not rocket science. If users if an array of users, then result = users.each do |user|    result << {:mail => user.mail, :name => user.name} end

will do the trick. Or slightly more ideomatically:

users.collect {|user| {:mail => user.mail, :name => user.name} }

that said most of the time I would just use the users array as is.

Fred

well im a noob but looks im getting closer. what i have now is

ldap.search( :base => treebase, :filter => filter ) do |entry|

# here is email john.doe@dot.com

emailstr = entry[:mail].to_s

# here is John's uid = john

uidstr = entry[:uid].to_s

# here is john's fullname

fullnamestr = entry[:cn].to_s

if emailstr != '' && uidstr != '' && fullnamestr != '' then @collection << {:mail => emailstr, :fullname => fullnamestr, :uid => uidstr} end end

and print to those out i have

<% @collection.each do |key,val| %> KEY: <%= key %> VAL: <%= val %><br> <% end %>

this results to this:

KEY: uidjohnfullnameJohnDoemailjohn.doe@dot.com VAL:

ie. i cant seem to separate those values but all comes as one huge string.

and btw thanks for the help so far.

well im a noob but looks im getting closer. what i have now is

<% @collection.each do |key,val| %> KEY: <%= key %> VAL: <%= val %><br> <% end %>

The members of the collection are hashes, but @collection isn't. You need to iterate over @collection, which yields you the hashes, and then you need to iterate over those to print the key value pairs. There's probably not much value in doing this as opposed to iterating over the entries returned by ldap.search

Fred

well ldapsearch works in controller only and from there I need to pass data to the view. This is too complicated for me since there are no good examples on the net. I just do big array and go those one by one thru.

well ldapsearch works in controller only and from there I need to pass data to the view. This is too complicated for me since there are no good examples on the net. I just do big array and go those one by one thru.

Creating some collection in a controller and then iterating over that
in a view is something that any tutorial app will cover. Sure most of
those collections would be ActiveRecord objects rather than ldap
thingies, but that doesn't make any difference.

All you need to do is

@ldap_entries = ldap.search( :base => treebase, :filter => filter ) in your controller, and

<% @ ldap_entries.each do |entry| %> ... <% end %>

Or <%= render :partial => 'entry', :collection => @ldap_entries %> and then have _entry.html.erb that looks something like

mail : <%= h entry.mail%> ...

Fred

omg it cant be that easy !!! yes yes it works thanks a lot man !!!