Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Confused with "include" and require

Reply
Thread Tools

Confused with "include" and require

 
 
Steve P.
Guest
Posts: n/a
 
      07-26-2010
I am getting an error when I include or require a file.

My code was getting too long for one file, so I attempted to separate
out one section into another file, and then "load" the file into the
main file at the appropriate place.

The main body of my ruby script is /home/holocene/ruby/q.rb
The load'ed file is /home/holocene/ruby/questionsaddquestions.rb

The error I am getting is:
/home/holocene/ruby/questionsaddquestions.rb:10: undefined local
variable or method `statequestions' for main:Object (NameError)
from ./q.rb:44:in `load'
from ./q.rb:44

The code before the load, creates the instance "statequestions", and the
code in the load'ed file acts upon the instance "statequestions".

The error tells me that the loaded text does not know the instance has
been created. (Proved because when I re-insert the loaded text, and #
out the load, it works)

Why am I getting an error? I thought "load" essentially is a substitute
for keyed text, as opposed to 'require'.



Best Regards,
Steve.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Florian Gilcher
Guest
Posts: n/a
 
      07-26-2010

On Jul 26, 2010, at 8:40 PM, Steve P. wrote:

> I am getting an error when I include or require a file.
>=20
> My code was getting too long for one file, so I attempted to separate
> out one section into another file, and then "load" the file into the
> main file at the appropriate place.
>=20
> The main body of my ruby script is /home/holocene/ruby/q.rb
> The load'ed file is /home/holocene/ruby/questionsaddquestions.rb
>=20
> The error I am getting is:
> /home/holocene/ruby/questionsaddquestions.rb:10: undefined local
> variable or method `statequestions' for main:Object (NameError)
> from ./q.rb:44:in `load'
> from ./q.rb:44
>=20
> The code before the load, creates the instance "statequestions", and =

the
> code in the load'ed file acts upon the instance "statequestions".
>=20
> The error tells me that the loaded text does not know the instance has
> been created. (Proved because when I re-insert the loaded text, and #
> out the load, it works)
>=20
> Why am I getting an error? I thought "load" essentially is a =

substitute
> for keyed text, as opposed to 'require'.


=46rom the documentation of 'load'[1]:

"In no circumstance will any local variables in the loaded file be =
propagated to the loading environment."

There are other, better ways to do this for example by separating your =
logic into classes and then get the program going from a small main =
program. Any tutorial about moderately complex ruby programs should =
illustrate how to do this.

require and load do not behave like PHPs #include at all.

Regards,
Florian

[1] http://ruby-doc.org/core/classes/Kernel.html#M005940=

 
Reply With Quote
 
 
 
 
Steve P.
Guest
Posts: n/a
 
      07-27-2010
Florian,

As you indicated, I put my classes in files and then require'd, them and
it works, of course.

I guess as I attain experience, the seeming restrictive way that
"require" works, will seem great. I was naive in thinking that arbitrary
code could be "include"-ed.

Best Regards
Steve.




Florian Gilcher wrote:
> On Jul 26, 2010, at 8:40 PM, Steve P. wrote:
>
>> /home/holocene/ruby/questionsaddquestions.rb:10: undefined local
>>
>> Why am I getting an error? I thought "load" essentially is a substitute
>> for keyed text, as opposed to 'require'.

>
> From the documentation of 'load'[1]:
>
> "In no circumstance will any local variables in the loaded file be
> propagated to the loading environment."
>
> There are other, better ways to do this for example by separating your
> logic into classes and then get the program going from a small main
> program. Any tutorial about moderately complex ruby programs should
> illustrate how to do this.
>
> require and load do not behave like PHPs #include at all.
>
> Regards,
> Florian
>
> [1] http://ruby-doc.org/core/classes/Kernel.html#M005940


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
confused between char and char* and connection to Arrays arnuld C++ 19 03-30-2007 04:42 AM
Firefox and RSS feeds that require authentication ttfg Firefox 0 03-25-2005 11:51 PM
DVD Verdict reviews: ULTIMATE PARTY COLLECTION: DAZED AND CONFUSED / FAST TIMES AT RIDGEMONT HIGH and more! DVD Verdict DVD Video 0 12-07-2004 10:11 AM
Problem with "require" and multiple modules. Don Perl 2 12-10-2003 10:15 AM
Require SPE 310s and MRP 300s - Anyone know a source? Pat Metheny Cisco 1 11-19-2003 10:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57