But I didn't notice any difference between `Kernel#load` call with the
second parameter and without. Probably my example is not one which can
answer the documentation- Module: Kernel (Ruby 2.0.0)
My point is, without the wrap=true the modification of @doc instance
variable inside "so.rb"
propagates back to the test.br context.
so.rb
···
======
require 'nokogiri'
@doc = Nokogiri::HTML.parse("<div>foo</div>")
puts "Inside so.rb"
p @doc
test.rb
@doc = "doc is Just a String as an 'original' value"
puts "Inside test.rb before load"
p @doc #=> It renders that String
load "./so.rb", true
puts "Inside test.rb after load with the wrap=true option"
p @doc #=> Still rendering a String
load "./so.rb"
puts "Inside test.rb after load"
p @doc #=> Now, without the wrap=true, your @doc points to a
Nokogiri::HTML::Document
# Because it was set this way "inside" called file "so.rb".
Abinoam Jr.
On Tue, Sep 24, 2013 at 3:04 PM, Love U Ruby <lists@ruby-forum.com> wrote:
Abinoam Jr. wrote in post #1122283:
Test it with an instance variable.
Without the wrap=true the @doc "contaminates" the caller file's context.
I tested with an instance variable,the result is same. I didn't get your
point.
My point is, without the wrap=true the modification of @doc instance
variable inside "so.rb"
propagates back to the test.br context.
so.rb
require 'nokogiri'
@doc = Nokogiri::HTML.parse("<div>foo</div>")
puts "Inside so.rb"
p @doc
test.rb
@doc = "doc is Just a String as an 'original' value"
puts "Inside test.rb before load"
p @doc #=> It renders that String
load "./so.rb", true
puts "Inside test.rb after load with the wrap=true option"
p @doc #=> Still rendering a String
load "./so.rb"
puts "Inside test.rb after load"
p @doc #=> Now, without the wrap=true, your @doc points to a
Nokogiri::HTML::Document
# Because it was set this way "inside" called file "so.rb".
I did the test...
kirti@kirti-Aspire-5733Z:~/ruby$ ruby test.rb
Inside test.rb before load
"doc is Just a String as an 'original' value"
Inside so.rb
#<Nokogiri::HTML::Document:0x44e72f4 name="document"
children=[#<Nokogiri::XML::DTD:0x44e71c8 name="html">,
#<Nokogiri::XML::Element:0x44e709c name="html"
children=[#<Nokogiri::XML::Element:0x44e6f5c name="body"
children=[#<Nokogiri::XML::Element:0x44e6dae name="div"
children=[#<Nokogiri::XML::Text:0x44e6b6a "foo">]>]>]>]>
Inside test.rb after load with the wrap=true option
"doc is Just a String as an 'original' value"
Inside so.rb
#<Nokogiri::HTML::Document:0x44e628c name="document"
children=[#<Nokogiri::XML::DTD:0x44e614c name="html">,
#<Nokogiri::XML::Element:0x459ffe8 name="html"
children=[#<Nokogiri::XML::Element:0x459fef8 name="body"
children=[#<Nokogiri::XML::Element:0x459fe08 name="div"
children=[#<Nokogiri::XML::Text:0x459fd18 "foo">]>]>]>]>
Inside test.rb after load
#<Nokogiri::HTML::Document:0x44e628c name="document"
children=[#<Nokogiri::XML::DTD:0x44e614c name="html">,
#<Nokogiri::XML::Element:0x459ffe8 name="html"
children=[#<Nokogiri::XML::Element:0x459fef8 name="body"
children=[#<Nokogiri::XML::Element:0x459fe08 name="div"
children=[#<Nokogiri::XML::Text:0x459fd18 "foo">]>]>]>]>
kirti@kirti-Aspire-5733Z:~/ruby$
If you load it with the wrap=true then there's a separate enviroment
and there will be two distinct @doc and there's no way you can
"access" that modified doc that is inside the so.rb
If you load it without the wrap=true then it's like they are one
single script file and the contexts are the same. So if you change in
one place it will be changed everywhere else.
Abinoam Jr.
···
On Tue, Sep 24, 2013 at 6:32 PM, Love U Ruby <lists@ruby-forum.com> wrote:
Abinoam Jr. wrote in post #1122322:
Could you repeat the test using the code bellow?
test.rb
@doc = "doc is Just a String as an 'original' value"
puts "Inside test.rb before load"
p @doc #=> It renders that String
load "./so.rb", true
puts "Inside test.rb after load with the wrap=true option"
p @doc #=> Still rendering a String
In this case how can we access the @doc of the loaded script?
If you load it without the wrap=true then it's like they are one
single script file and the contexts are the same. So if you change in
one place it will be changed everywhere else.
The above sentences helped me a lot to understand the docs explanations
- If the optional wrap parameter is true, the loaded script will be
executed under an **anonymous module**, protecting the calling program’s
**global namespace.**