为什么我使用Redcloth获得“错误的参数数量(3个为2)”?

时间:2013-07-16 16:26:55

标签: ruby redcloth

我正在尝试在我的Ruby应用程序中使用Redcloth,即使我只有两个参数,它也会给我wrong number of arguments (3 for 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(/&nbsp;/," ")
    redcloth.clean_html(string, {})
    string
  end
end

这是源代码:http://redcloth.rubyforge.org/classes/RedCloth/Formatters/HTML.html#M000052

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'

1 个答案:

答案 0 :(得分:0)

您收到错误是因为Redcloth源文件(base.rb)中method_missing的定义只用两个参数定义(并且没有splat运算符)并且您正在使用两个参数调用未定义的方法当添加到方法名称时,会将三个参数传递给method_missing

违规通话似乎是对redcloth.clean_html的通话。正如您在评论中指出的那样,clean_html是一种私有方法,因此您无法通过正常的object.method调用机制访问它。