给定参数的Ruby模块调用一个方法?

时间:2020-12-19 23:19:36

I'm confused about what's going on in the Nokogiri docs.

我对Nokogiri文档中发生的事情感到困惑。

As far as I can tell, if

据我所知,如果

require 'nokogiri'
some_html = "<html><body><h1>Mr. Belvedere Fan Club</h1></body></html>"

then these three lines do the same thing:

然后这三行做同样的事情:

html_doc = Nokogiri::HTML::Document.parse(some_html)
html_doc = Nokogiri::HTML.parse(some_html)
html_doc = Nokogiri::HTML(some_html)

The second is just a convenience method for the first. But to my non-Ruby eyes, the third looks like it's passing an argument to a module, not a method. I realize that Ruby has constructors, but I thought they took the form Class.new, not Module(args). What's going on here?

第二个是第一个方便的方法。但对于我的非Ruby眼睛,第三个看起来像是将一个参数传递给一个模块,而不是一个方法。我意识到Ruby有构造函数,但我认为它们采用的形式是Class.new,而不是Module(args)。这里发生了什么?

1 个解决方案

#1


8  

It's just syntax sugar, look at the Nokogiri::HTML module definition:

它只是语法糖,看看Nokogiri :: HTML模块定义:

module Nokogiri
  class << self
    ###
    # Parse HTML.  Convenience method for Nokogiri::HTML::Document.parse
    def HTML thing, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_HTML, &block
      Nokogiri::HTML::Document.parse(thing, url, encoding, options, &block)
    end
  end

  module HTML
    class << self
      ###
      # Parse HTML.  Convenience method for Nokogiri::HTML::Document.parse
      def parse thing, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_HTML, &block
        Document.parse(thing, url, encoding, options, &block)
      end

      ####
      # Parse a fragment from +string+ in to a NodeSet.
      def fragment string, encoding = nil
        HTML::DocumentFragment.parse string, encoding
      end
    end

    # Instance of Nokogiri::HTML::EntityLookup
    NamedCharacters = EntityLookup.new
  end
end

First, they define a class method at the Nokogiri module called HTML (yes, Ruby allows you to do that), then they define the module Nokogiri::HTML and in there they define the class method parse.

首先,他们在Nokogiri模块中定义一个名为HTML的类方法(是的,Ruby允许你这样做),然后他们定义模块Nokogiri :: HTML并在那里定义类方法解析。

Most people don't know but the :: operator can also be used to perform method calls:

大多数人都不知道,但::运算符也可用于执行方法调用:

"my_string"::size #will print 9

#1


8  

It's just syntax sugar, look at the Nokogiri::HTML module definition:

它只是语法糖,看看Nokogiri :: HTML模块定义:

module Nokogiri
  class << self
    ###
    # Parse HTML.  Convenience method for Nokogiri::HTML::Document.parse
    def HTML thing, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_HTML, &block
      Nokogiri::HTML::Document.parse(thing, url, encoding, options, &block)
    end
  end

  module HTML
    class << self
      ###
      # Parse HTML.  Convenience method for Nokogiri::HTML::Document.parse
      def parse thing, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_HTML, &block
        Document.parse(thing, url, encoding, options, &block)
      end

      ####
      # Parse a fragment from +string+ in to a NodeSet.
      def fragment string, encoding = nil
        HTML::DocumentFragment.parse string, encoding
      end
    end

    # Instance of Nokogiri::HTML::EntityLookup
    NamedCharacters = EntityLookup.new
  end
end

First, they define a class method at the Nokogiri module called HTML (yes, Ruby allows you to do that), then they define the module Nokogiri::HTML and in there they define the class method parse.

首先,他们在Nokogiri模块中定义一个名为HTML的类方法(是的,Ruby允许你这样做),然后他们定义模块Nokogiri :: HTML并在那里定义类方法解析。

Most people don't know but the :: operator can also be used to perform method calls:

大多数人都不知道,但::运算符也可用于执行方法调用:

"my_string"::size #will print 9