Difference between `Kernel#load` call with the second parameter and without

I was trying to understand the difference between `Kernel#load` with the
second parameter and without.

I wrote the code :-

so.rb

···

=====

require 'nokogiri'

doc = Nokogiri::HTML.parse("<div>foo</div>")
p doc.child

test.rb

load 'C:/Users/rakshiar/Documents/userdata/so.rb',true

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-
http://www.ruby-doc.org/core-2.0.0/Kernel.html#method-i-load

I went thorough -
http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/
and


. But those doesn't answer my question.

--
Posted via http://www.ruby-forum.com/.

Test it with an instance variable.

Without the wrap=true the @doc "contaminates" the caller file's context.

···

On Tue, Sep 24, 2013 at 12:48 PM, Love U Ruby <lists@ruby-forum.com> wrote:

I was trying to understand the difference between `Kernel#load` with the
second parameter and without.

I wrote the code :-

so.rb

require 'nokogiri'

doc = Nokogiri::HTML.parse("<div>foo</div>")
p doc.child

test.rb

load 'C:/Users/rakshiar/Documents/userdata/so.rb',true

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)

I went thorough -
Ruby Require VS Load VS Include VS Extend | I On Rails
and
How does load differ from require in Ruby? - Stack Overflow
. But those doesn't answer my question.

--
Posted via http://www.ruby-forum.com/\.

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.

···

--
Posted via http://www.ruby-forum.com/\.

I did what you said. But see the output of mine.

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:0x481cfb4 name="document"
children=[#<Nokogiri::XML::DTD:0x481ce74 name="html">,
#<Nokogiri::XML::Element:0x481ccf8 name="html"
children=[#<Nokogiri::XML::Element:0x481cbea name="body"
children=[#<Nokogiri::XML::Element:0x481caaa name="div"
children=[#<Nokogiri::XML::Text:0x481c992 "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:0x481c3d4 name="document"
children=[#<Nokogiri::XML::DTD:0x481c294 name="html">,
#<Nokogiri::XML::Element:0x481c0d2 name="html"
children=[#<Nokogiri::XML::Element:0x481d126 name="body"
children=[#<Nokogiri::XML::Element:0x4815b92 name="div"
children=[#<Nokogiri::XML::Text:0x4815a5c "foo">]>]>]>]>
Inside test.rb after load
#<Nokogiri::HTML::Document:0x481c3d4 name="document"
children=[#<Nokogiri::XML::DTD:0x481c294 name="html">,
#<Nokogiri::XML::Element:0x481c0d2 name="html"
children=[#<Nokogiri::XML::Element:0x481d126 name="body"
children=[#<Nokogiri::XML::Element:0x4815b92 name="div"
children=[#<Nokogiri::XML::Text:0x4815a5c "foo">]>]>]>]>
kirti@kirti-Aspire-5733Z:~/ruby$

···

--
Posted via http://www.ruby-forum.com/.

Could you repeat the test using the code bellow?

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.

--
Posted via http://www.ruby-forum.com/\.

Abinoam Jr. wrote in post #1122322:

Could you repeat the test using the code bellow?

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$

···

--
Posted via http://www.ruby-forum.com/\.

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?

···

--
Posted via http://www.ruby-forum.com/\.

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?

--
Posted via http://www.ruby-forum.com/\.

Abinoam Jr. wrote in post #1122359:

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.**

Thank you very much!!!

···

--
Posted via http://www.ruby-forum.com/\.