ruby-在initialize方法中插入一个数组-未定义的方法'join';用于:字符串(NoMethodError)
我尝试将数组作为参数传递,并在ERB文件中使用它ruby-在initialize方法中插入一个数组-未定义的方法'join';用于:字符串(NoMethodError),ruby,erb,Ruby,Erb,我尝试将数组作为参数传递,并在ERB文件中使用它 class Erbfile def initialize ec2 @ec2 = ec2 #@ec2 = ["Cleveland", "Denver", "Nashville"] #this one work @template = File.read('./file.erb') end def render ERB.new(@template).result( binding ) end end
class Erbfile
def initialize ec2
@ec2 = ec2
#@ec2 = ["Cleveland", "Denver", "Nashville"] #this one work
@template = File.read('./file.erb')
end
def render
ERB.new(@template).result( binding )
end
end
ec2=[]
ec2.instances.each do |instance|
tag_name = Facter::Util::Resolution.exec("...")
mystring = "#{tag_name}"
ec2 << mystring
puts "#{ec2_without_tags}" ### ["test.ec2.mycomany.int"]
@page = Erbfile.new("#{ec2}")
end
但是,如果我将手动插入一个数组到initialize方法中,它就会工作
@ec2 = ["Cleveland", "Denver", "Nashville"]
我做错了什么?您正在将数组的字符串表示形式传递到Erbfile类中。然后对该字符串调用
.join
如果希望在类中接收数组,则需要传入数组
将
Erbfile.new(“#{ec2}”)
替换为Erbfile.new(ec2)
如错误所述,没有方法。代码可能需要一个数组;这个错误显示了某种(JSON)数组的字符串表示形式——请修复它。值得注意的是,像“#{x}”
这样的模式几乎总是错误的。按原样传递该变量。如果需要转换为字符串:x.to\u s
。
(erb):20:in `render': undefined method `join' for "[\"test.ec2.mycomany.int\"]":String (NoMethodError)
Did you mean? JSON
@ec2 = ["Cleveland", "Denver", "Nashville"]