【问题标题】:Why do I get "wrong number of arguments (3 for 2)" using Redcloth?为什么我使用 Redcloth 得到“错误数量的参数(3 比 2)”?
【发布时间】:2013-07-16 16:26:55
【问题描述】:

我正在尝试在我的 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'

【问题讨论】:

    标签: ruby redcloth


    【解决方案1】:

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

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

    【讨论】:

    • 嘿,彼得,我想你在做点什么。我想我不正确地包含了模块。如何在我的方法中包含 RedCloth::Formatters::HTML 并调用它?
    • 在混入 Ruby 方面,我没有达到应有的速度,但如果您只想使用 gem,通常只需 require源文件。由于您使用的是include,因此您实际上是将这些方法混合到您当前的类定义中,这不是您想要的。
    • 您遵循哪些文档来确定如何使用 gem?
    • 我只是在写一个小类,所以如果我在我的类中包含该模块就可以了。我正在使用这个:redcloth.rubyforge.org/classes/RedCloth/Formatters/HTML.html
    • 对于我在方法调用问题上的持续误报,我深表歉意。我可以看到RedCloth.new 是创建TextileDoc 的便捷方法,它确实有一个html_esc 实例方法。所以,我认为问题在于clean_html 调用,我认为它不是TextileDoc 的实例方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多