我得到了一个未定义的方法'下面的get_nums方法有错误,我无法弄清楚原因。 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
答案 0 :(得分:3)
看来,您已经因缩进而感到困惑,并且没有通过end
正确关闭这些方法。
我认为以下代码是您尝试实施的代码的正确版本:
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 = []
end
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
self
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
答案 1 :(得分:0)
由于您为pip install -r requirements.txt
定义了attr_accessor
,因此您不需要hosts
。你可以这么做:
self.hosts