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(“

foo
”)
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
http://stackoverflow.com/questions/3170638/how-does-load-differ-from-require-in-ruby
. But those doesn’t answer my question.

Test it with an instance variable.

Without the wrap=true the @doc “contaminates” the caller file’s context.

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.

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(“

foo
”)
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…

[email protected]:~/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”>]>]>]>]>
[email protected]:~/ruby$

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(“

foo
”)
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.

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.

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?

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!!!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs