# Hash key value pairs

**URL:** <https://www.ruby-forum.com/t/hash-key-value-pairs/128947>\
**Category:** Rails\
**Created:** [February 12, 2008, 10:02pm UTC](https://www.ruby-forum.com/t/hash-key-value-pairs/128947 "2008-02-12T22:02:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 12, 2008, 10:02pm UTC](https://www.ruby-forum.com/t/hash-key-value-pairs/128947/1 "2008-02-12T22:02:37Z")

</div>

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](http://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](http://tom.dot.com)”,  
“name” =\> “diana”, "uid=“101”, “mail” =\> “[diana.dot.com](http://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 …

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 12, 2008, 10:12pm UTC](https://www.ruby-forum.com/t/hash-key-value-pairs/128947/2 "2008-02-12T22:12:02Z")

</div>

On 12 Feb 2008, at 21:01, KTU wrote:

> Now I have function which loops thru 100 users and I would need to  
> 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

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 12, 2008, 10:17pm UTC](https://www.ruby-forum.com/t/hash-key-value-pairs/128947/3 "2008-02-12T22:17:54Z")

</div>

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

On Feb 12, 11:11 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 12, 2008, 10:36pm UTC](https://www.ruby-forum.com/t/hash-key-value-pairs/128947/4 "2008-02-12T22:36:11Z")

</div>

On 12 Feb 2008, at 21:16, KTU wrote:

> 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

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 12, 2008, 11:30pm UTC](https://www.ruby-forum.com/t/hash-key-value-pairs/128947/5 "2008-02-12T23:30:54Z")

</div>

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 removed\_email\_address@domain.invalid

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 %\>  
  
\<% end %\>

this results to this:

KEY: removed\_email\_address@domain.invalid VAL:

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

and btw thanks for the help so far.

On Feb 12, 11:35 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 13, 2008, 8:38am UTC](https://www.ruby-forum.com/t/hash-key-value-pairs/128947/6 "2008-02-13T08:38:49Z")

</div>

On 12 Feb 2008, at 22:30, KTU wrote:

> well im a noob but looks im getting closer. what i have now is
> 
> \<% @collection.each do |key,val| %\>  
> KEY: \<%= key %\> VAL: \<%= val %\>  
>   
> \<% 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

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 13, 2008, 9:41am UTC](https://www.ruby-forum.com/t/hash-key-value-pairs/128947/7 "2008-02-13T09:41:14Z")

</div>

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.

On Feb 13, 9:37 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 13, 2008, 9:49am UTC](https://www.ruby-forum.com/t/hash-key-value-pairs/128947/8 "2008-02-13T09:49:31Z")

</div>

On 13 Feb 2008, at 08:40, KTU wrote:

> 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

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 13, 2008, 10:57am UTC](https://www.ruby-forum.com/t/hash-key-value-pairs/128947/9 "2008-02-13T10:57:47Z")

</div>

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

On Feb 13, 10:48 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
