开发者

Ruby Net:LDAP- NoMethodError for attributes that don't exist

开发者 https://www.devze.com 2023-01-15 06:48 出处:网络
I\'m doing a simple Net:LDAP search and when I\'m outputting an entry\'s attribute that may not exist for every entry, I get an error \"NoMethodError: undefined method \'some_attribute\'\"

I'm doing a simple Net:LDAP search and when I'm outputting an entry's attribute that may not exist for every entry, I get an error "NoMethodError: undefined method 'some_attribute'"

Here is the code:

require 'rubygems'
require 'net/ldap'

ldap = Net::LDAP.new
ldap.host = 'ldap.example.com'
ldap.port = 389
if ldap.bind
  filter = Net::LDAP::Filter.eq( "sn", "Smith" )
  treebase = "ou=people,o=company"
  ldap.search( :base => treebase, :filter => filter, :return_result => false) do |entry|
    puts #{entry.some_attribute}
  end
end
else
  puts "bind unsuccessful"
end

I tried also doing:

if entry.respond_to?(some_att开发者_JAVA百科ribute)
  puts "#{entry.some_attribute}"
end

That didn't work, it returns as false for every entry (when some entries have the attribute).


Ruby is expecting a symbol in the respond_to? method call.

ruby-1.8.7-p299 > class Foo
ruby-1.8.7-p299 ?>  attr_accessor :some_attr
ruby-1.8.7-p299 ?>  end
 => nil 
ruby-1.8.7-p299 > Foo.new.respond_to?(some_attr)
NameError: undefined local variable or method `some_attr' for #<Object:0xb77ce950>
    from (irb):4
ruby-1.8.7-p299 > Foo.new.respond_to?(:some_attr)
 => true 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号