31e2b397e15442a2d3ba42db7bd4584e

The code below gives me what I want, but is quite verbose. I'm looking for a cleaner way.

# Array of LDAP group names
@group_names = []

# Query the server (ldap holds a valid connection to the server
@groups = ldap.get_groups("DC=example,DC=com")

# @groups contains an array of hashes.  All I need is the cn attribute.
@groups.select {|group| @group_names << group[:cn] } 

# I now have an array of arrays, so I flatten into a single dimension array
@group_names.flatten!

Refactorings

No refactoring yet !

A8d3f35baafdaea851914b17dae9e1fc

Adam

August 9, 2010, August 09, 2010 23:33, permalink

1 rating. Login to rate!
@groups = ldap.get_groups("DC=example,DC=com")
@group_names = @groups.map { |group| group[:cn ] }.flatten!

Your refactoring





Format Copy from initial code

or Cancel