Ruby:扩展模块的类

发布于 2024-07-25 16:45:12 字数 494 浏览 4 评论 0原文

我试图定义一个名为“HTML”的类来扩展 Nokogiri - 它使用模块。

我尝试了以下方法:

require 'nokogiri'

class HTML
    include Nokogiri
end

require 'nokogiri'

class HTML
    extend Nokogiri
end

到目前为止,HTML 类不可能继承 nokogiri 中的所有功能。 所以类似的东西不起作用:

doc = HTML.new
doc.HTML(open('http://www.google.com/search?q=tenderlove'))

undefined method `HTML' for # (NoMethodError)

有谁知道我如何能够编写一个继承模块所有方法的类?

I'm trying to to define a class called "HTML" to extend Nokogiri - which uses modules.

I tried the following:

require 'nokogiri'

class HTML
    include Nokogiri
end

and

require 'nokogiri'

class HTML
    extend Nokogiri
end

but so far it's been impossible that the class HTML inherit of all the functions in nokogiri. So stuff like are not working:

doc = HTML.new
doc.HTML(open('http://www.google.com/search?q=tenderlove'))

undefined method `HTML' for # (NoMethodError)

Does anyone know how I could manage to program a class that inherits of all the methods of a module?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

书间行客 2024-08-01 16:45:13

Nokogiri 没有任何可以继承的实例方法:

irb> Nokogiri.instance_methods
#=>  []

但通常,您会使用 extend

% ri extend
---------------------------------------------------------- Object#extend
     obj.extend(module, ...)    => obj
------------------------------------------------------------------------
     Adds to obj the instance methods from each module given as a
     parameter.

        module Mod
          def hello
            "Hello from Mod.\n"
          end
        end

        class Klass
          def hello
            "Hello from Klass.\n"
          end        end

        k = Klass.new
        k.hello         #=> "Hello from Klass.\n"
        k.extend(Mod)   #=> #<Klass:0x401b3bc8>
        k.hello         #=> "Hello from Mod.\n"

%

您想要做的是将 Nokogiri 模块的所有类方法用作您的类的实例方法。 这有点不标准,这就是语法不支持它的原因。 大多数程序员使用 ruby​​ 模块来实现单例模式——只需要一个 Nokogiri,所以其他东西不应该能够使用它的方法。

您可以使用 UndefinedMethods 进行一些黑客攻击来解决这个问题,但考虑到 Nokogiri 在后端有一些编译代码,这可能会产生未定义的错误。

这并不是说您不能将调用转发到 Nokogiri:

# nokogiri_wrapper.rb
require 'rubygems'
require 'nokogiri'

class NokogiriWrapper
  def method_missing(meth, *args, &blk)
    puts "call for #{meth.inspect}, #{args}, #{blk ? "with block" : "and no block"}"
    if Nokogiri.methods.include? meth.to_s
      puts "forwarding to Nokogiri"
      Nokogiri.send(meth, *args, &blk)
    else
      puts "falling back to default behaviour"
      super
    end
  end
end

html = "<html></html>"

puts "calling Nokogiri directly"
p Nokogiri.HTML(html)

wrapper = NokogiriWrapper.new

puts "calling Nokogiri through wrapper"
p wrapper.HTML(html)

puts "calling non-Nokogiri method with wrapper"
p(begin
    wrapper.scooby_dooby_doo!
  rescue NoMethodError => e
    [e.message, e.backtrace]
  end)
% ruby nokogiri_wrapper.rb
calling Nokogiri directly
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html></html>

calling Nokogiri through wrapper
call for :HTML, <html></html>, and no block
forwarding to Nokogiri
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html></html>

calling non-Nokogiri method with wrapper
call for :scooby_dooby_doo!, , and no block
falling back to default behaviour
["undefined method `scooby_dooby_doo!' for #<NokogiriWrapper:0x581f74>", ["nokogiri_wrapper.rb:12:in `method_missing'", "nokogiri_wrapper.rb:29"]]

这是在 ruby​​ 中实现委托者模式的一种方法(另一种方法是使用 Delegator 类之一)。

Nokogiri doesn't have any instance methods to inherit:

irb> Nokogiri.instance_methods
#=>  []

But normally, you would use extend

% ri extend
---------------------------------------------------------- Object#extend
     obj.extend(module, ...)    => obj
------------------------------------------------------------------------
     Adds to obj the instance methods from each module given as a
     parameter.

        module Mod
          def hello
            "Hello from Mod.\n"
          end
        end

        class Klass
          def hello
            "Hello from Klass.\n"
          end        end

        k = Klass.new
        k.hello         #=> "Hello from Klass.\n"
        k.extend(Mod)   #=> #<Klass:0x401b3bc8>
        k.hello         #=> "Hello from Mod.\n"

%

What you want to do is use all the class methods of the Nokogiri module as instance methods of your class. Which is a bit nonstandard, which is why the syntax doesn't support it. Most programmers use ruby modules for the Singleton pattern - there only needs to be one Nokogiri, so other things shouldn't be able to use its methods.

You could do some hacking with UndefinedMethods to get around this, but considering that Nokogiri has some compiled code in the backend, this may produce undefined bugs.

Which isn't to say you can't forward calls to Nokogiri:

# nokogiri_wrapper.rb
require 'rubygems'
require 'nokogiri'

class NokogiriWrapper
  def method_missing(meth, *args, &blk)
    puts "call for #{meth.inspect}, #{args}, #{blk ? "with block" : "and no block"}"
    if Nokogiri.methods.include? meth.to_s
      puts "forwarding to Nokogiri"
      Nokogiri.send(meth, *args, &blk)
    else
      puts "falling back to default behaviour"
      super
    end
  end
end

html = "<html></html>"

puts "calling Nokogiri directly"
p Nokogiri.HTML(html)

wrapper = NokogiriWrapper.new

puts "calling Nokogiri through wrapper"
p wrapper.HTML(html)

puts "calling non-Nokogiri method with wrapper"
p(begin
    wrapper.scooby_dooby_doo!
  rescue NoMethodError => e
    [e.message, e.backtrace]
  end)
% ruby nokogiri_wrapper.rb
calling Nokogiri directly
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html></html>

calling Nokogiri through wrapper
call for :HTML, <html></html>, and no block
forwarding to Nokogiri
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html></html>

calling non-Nokogiri method with wrapper
call for :scooby_dooby_doo!, , and no block
falling back to default behaviour
["undefined method `scooby_dooby_doo!' for #<NokogiriWrapper:0x581f74>", ["nokogiri_wrapper.rb:12:in `method_missing'", "nokogiri_wrapper.rb:29"]]

This is one way to implement the delegator pattern in ruby (another way is to use one of the Delegator classes).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文