Love U Ruby <[email protected]> wrote:
> You realized my exact pain area. I always confused about the use of the 
> below two methods:
> 
> **Nokogiri::HTML::Document.new

Use when creating a new (i.e. NON-EXISTANT) HTML document.

> **Nokogiri::HTML::Document.parse

Use when parsing an existing COMPLETE HTML document, but NOT a fragment.

Use Nokogiri::HTML::DocumentFragment.parse when parsing a fragment
string (i.e., INCOMPLETE DOCUMENT).

> ==================================
> 
> 
> require "nokogiri"
> require 'pp'
> 
> doc = Nokogiri::HTML::Document.parse "<title> Save the page! </title>"
> doc.class # => Nokogiri::HTML::Document
> doc
> # => #(Document:0x4592d16 {
> #      name = "document",
> #      children = [
> #        #(DTD:0x4592244 { name = "html" }),
> #        #(Element:0x458dd7a {
> #          name = "html",
> #          children = [
> #            #(Element:0x45871d2 {
> #              name = "head",
> #              children = [
> #                #(Element:0x458161a {
> #                  name = "title",
> #                  children = [ #(Text " Save the page! ")]
> #                  })]
> #              })]
> #          })]
> #      })
> 
> doc = Nokogiri::HTML::Document.new("<title> Save the page! </title>")
> doc.class # => Nokogiri::HTML::Document
> doc
> # => #(Document:0x4578128 {
> #      name = "document",
> #      children = [ #(DTD:0x45714fe { name = "html" })]
> #      })
> 
> 
> Both the document creating `Nokogiri::HTML::Document` object. But when I 
> am printing those,seeing the output differently. Now my questions are -
> 
> (a) why does `Nokogiri::HTML::Document.parse` and 
> `Nokogiri::HTML::Document.new` creating the document object differently?

As stated above, these do two different things, although you may end up
with the same class of object, HOW they go about getting there is
different.

> (b) What is the proper use-case about their uses mean when should I need 
> to think/what method to use?

See above.

> Please help me to digest this basic food. :) :)

Pre-chewed and partially digested.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/51b376d1.a255b60a.5252.2b4a%40mx.google.com?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to