Ruby 为什么我会得到;参数数目错误(3对2)“;用红布?
我试图在我的Ruby应用程序中使用Redcloth,它给了我一个Ruby 为什么我会得到;参数数目错误(3对2)“;用红布?,ruby,redcloth,Ruby,Redcloth,我试图在我的Ruby应用程序中使用Redcloth,它给了我一个错误的参数数(3对2)错误,尽管我只有两个参数 这是我的密码: def self.cleanup(string) if string == "" || string == nil "" else string = self.iconvert(string, "ascii", "utf8") RedCloth.include(RedCloth::Formatters::HTML)
错误的参数数(3对2)
错误,尽管我只有两个参数
这是我的密码:
def self.cleanup(string)
if string == "" || string == nil
""
else
string = self.iconvert(string, "ascii", "utf8")
RedCloth.include(RedCloth::Formatters::HTML)
redcloth = RedCloth.new(string)
redcloth.html_esc(string)
string = string.strip
string = string.gsub(/''/,"\"")
string = string.gsub(/\r/," ")
string = string.gsub(/\n/," ")
string = string.gsub(/\<br \/\>/," ")
string = string.gsub(/\<br\/\>/," ")
string = string.gsub(/ /," ")
redcloth.clean_html(string, {})
string
end
end
之所以会出现错误,是因为该Redcloth源文件(base.rb)中的
method\u missing
定义仅使用两个参数(并且没有splat运算符)进行定义,并且您正在使用两个参数调用一个未定义的方法,当添加到方法名称时,结果三个参数被传递到方法\u missing
有问题的调用似乎是对
redcloth.clean\u html
的调用。正如您在评论中所指出的,clean_html
是一个私有方法,因此您无法通过正常的object.method
调用机制访问它。嘿,彼得,我想您已经了解了一些事情。我想我把这个模块包括得不恰当。我如何在我的方法中包含并调用RedCloth::Formatters::HTML
?我并没有像我在Ruby中所做的那样快,但是如果你只想使用gem,你通常只需要需要
源文件。由于您使用的是include
,因此您将这些方法有效地混合到当前的类定义中,而这并不是您想要的。您遵循哪些文档来确定如何使用gem?我只编写了一个小类,因此如果我在类中包含模块就可以了。我使用的是:对于我在方法调用问题上持续出现的错误警报,我深表歉意。我可以看到RedCloth.new
是创建TextileDoc
的一种方便方法,它确实有一个html\u esc
实例方法。因此,我认为问题在于clean_html
调用,我认为这不是TextileDoc
的实例方法。
def clean_html( text, allowed_tags = BASIC_TAGS )
text.gsub!( /<!\[CDATA\[/, '' )
text.gsub!( /<(\/*)([A-Za-z]\w*)([^>]*?)(\s?\/?)>/ ) do |m|
raw = $~
tag = raw[2].downcase
if allowed_tags.has_key? tag
pcs = [tag]
allowed_tags[tag].each do |prop|
['"', "'", ''].each do |q|
q2 = ( q != '' ? q : '\s' )
if raw[3] =~ /#{prop}\s*=\s*#{q}([^#{q2}]+)#{q}/i
attrv = $1
next if (prop == 'src' or prop == 'href') and not attrv =~ %r{^(http|https|ftp):}
pcs << "#{prop}=\"#{attrv.gsub('"', '\\"')}\""
break
end
end
I, [2013-07-16T12:22:53.095073 #12321] INFO -- : wrong number of arguments (3 for 2)
I, [2013-07-16T12:22:53.095281 #12321] INFO -- : /home/emai/.rvm/gems/ruby-1.9.3-p448@edmund/gems/RedCloth-4.2.9/lib/redcloth/formatters/base.rb:46:in `method_missing'