Ruby
Refactor array of hashes
# 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!
# 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 !
August 9, 2010,
August 09, 2010 23:33,
permalink
@groups = ldap.get_groups("DC=example,DC=com")
@group_names = @groups.map { |group| group[:cn ] }.flatten!
The code below gives me what I want, but is quite verbose. I'm looking for a cleaner way.