我在下面的get_nums方法中收到'undefined method'錯誤,我找不到原因。 list_hostnames返回一個數組,但get_nums不能對它做任何事情。有人能指點我正確的方向嗎?未定義的方法試圖匹配字符串
class Hostname
attr_accessor :hostname, :domain_controller_ip, :username, :password, :hosts
def initialize(hostname, domain_controller_ip, ad_username, ad_password)
@domain_controller_ip = domain_controller_ip
@ad_username = ad_username
@ad_password = ad_password
@hostname = hostname
@hosts = []
def list_hostnames
a = Ldap.new(@domain_controller_ip, @ad_username, @ad_password)
hostname = @hostname + "*"
a.ldap_con.search(:base => a.treebase, :filter => a.filter('cn', hostname)) do |entry|
self.hosts.push(entry.cn[0])
end
self.hosts.each do |x|
p x
end
end
def get_nums
self.hosts.each do |x|
i = x.match(/\d+$/)
p i
end
end
end
a = Hostname.new('prod-srv-1', '192.168.1.1', 'administrator', 'password')
b = a.list_hostnames
b.get_nums
end
也是你'#initialize'方法缺少一個'end' –