我试图用.delete_if从数组中删除某些元素。例如,我希望从数组中删除以下内容
[83]#[“CN = hhtest,OU =用户XXX,DC = XXX,DC = com”], :cn => [“hhtest”],:sn => [“test”],:givenname => [“hh”], :的memberOf => [ “CN = SSLVPN用户,CN =用户,DC = XXX,DC = COM”, “CN =管理员,CN =内藏,DC = XXX,DC = COM”], :的UserPrincipalName => [ “hhtest@XXX.com”]}> ]]
我试图用
删除它 @ldap_result.delete_if { |cn| cn == 'hhtest' }
我也尝试过其他变种。注意 - 如果我在视图中执行@ ldap_result.class,它将返回`array'
如果我这样做
@ldap_result.delete_if { |cn| cn != 'hhtest' }
我最终得到一个似乎违反直觉的空数组。
答案 0 :(得分:0)
对我的问题的评论让我觉得我走错了路。我最终没有使用delete_if。我做的是遍历从ldap直接提取的名称列表,然后检查ldap_exceptions表中是否有匹配的条目。我知道这不是很有效但我们最多只有100个用户可以通过,任何时候只有3或4个人会使用该应用程序。
# ---------------------------------
# this is where we build the operator list. The operator list is the users pulled from LDAP minus any user
# names entered in the ldap exceptions table. Note - Case and spacing must match exactly with the users
# you want to exclude.
@ldap_exceptions = LdapException.all # get all of the users in ldap exceptions.
@operator_list = ["x"] # initialize @operator_list as an array.
@ldap_names.each do |ldn| # ldap names comes from the application controller.
@ldap_exception = LdapException.where("username like ?", ldn) # See if there is an entry in the ldap exception table. This isn't very efficient but considering the number of ldap users,
# it should be fine.
if !@ldap_exception.present? then # if the ldap user isn't in the ldap exceptions table write it to the array
@operator_list = @operator_list + [ldn] # ldn has to be in [] or you get a string into an array error.
end
end
简而言之,我试图在一个哈希而不是数组的东西上做一个delete_if。这种方法有效。