Confused with "include" and require

Discussion in 'Ruby' started by Steve P., Jul 26, 2010.

  1. Steve P.

    Steve P. Guest

    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/.
     
    Steve P., Jul 26, 2010
    #1
    1. Advertising

  2. 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=
     
    Florian Gilcher, Jul 26, 2010
    #2
    1. Advertising

  3. Steve P.

    Steve P. Guest

    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/.
     
    Steve P., Jul 27, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Bogenberger
    Replies:
    3
    Views:
    937
    Andreas Bogenberger
    Feb 22, 2008
  2. Chad M.
    Replies:
    2
    Views:
    135
    Chad M.
    Mar 27, 2009
  3. explainer
    Replies:
    0
    Views:
    73
    explainer
    Jul 11, 2009
  4. R. Kumar
    Replies:
    3
    Views:
    139
    Brian Candler
    Mar 12, 2010
  5. rantingrick

    require and include??

    rantingrick, Mar 30, 2010, in forum: Ruby
    Replies:
    7
    Views:
    152
    Brian Candler
    Mar 30, 2010
Loading...

Share This Page