require

Discussion in 'Ruby' started by Piet Versteegen, Jul 12, 2006.

  1. I am using require in two different ways and they both don't work.
    In case #1 I use it to read a file with a code snippet. The file is
    located in the same directory. The code snipped defines an array
    called `code_words'. When trying to print it it says"
    "undefined local variable or method `code_words' for main:Object
    (NameError)"

    In the second case I have installed rubycoca and I'm trying to run the
    sample problem. I has a "require osx/cocoa" statement. The error
    message I get is "in `require': no such file to load -- osx/
    cocoa (LoadError)"

    Searching the Internet I found that other people have the same second
    problem, but no answer was found on how to solve it.

    Can anyone help?

    Thanks ahead of time.

    --
    peleve
     
    Piet Versteegen, Jul 12, 2006
    #1
    1. Advertisements

  2. Piet Versteegen

    Steve Martin Guest

    Are you defining the code_words variable as a local or instance
    variable. You should do:

    bash> cat code.rb
    @code_words = [ 'word1', 'word2' ]

    bash> cat word.rb
    #!/usr/bin/env ruby

    require 'code'
    @code_words += [ 'word3' ]
    puts @code_words

    bash> ruby word.rb
    word1
    word2
    word3

    --
    Posted via http://www.ruby-forum.com/.
     
    Steve Martin, Jul 12, 2006
    #2
    1. Advertisements

  3. Piet Versteegen

    ChrisH Guest

    Piet Versteegen wrote:
    > I am using require in two different ways and they both don't work.
    > In case #1 I use it to read a file with a code snippet. The file is
    > located in the same directory. The code snipped defines an array
    > called `code_words'. When trying to print it it says"
    > "undefined local variable or method `code_words' for main:Object
    > (NameError)"


    Be easier to answer if you provided some code.

    The issue is the local defined in the external file is not accessable
    in the scope where it is required. You can make the array global (call
    it '$code_words'), or a constant ('CODE_WORDS') or put it inside a
    module/class and access it appropriately.


    >
    > In the second case I have installed rubycoca and I'm trying to run the
    > sample problem. I has a "require osx/cocoa" statement. The error
    > message I get is "in `require': no such file to load -- osx/
    > cocoa (LoadError)"
    >
    > Searching the Internet I found that other people have the same second
    > problem, but no answer was found on how to solve it.
    >

    That should just be a matter of ensuring the path to the osx dir is in
    the library path.
    You can speciy the path in the RUBYLIB environment variable, or on the
    ruby command line using -I or in your code by adding the path to $:

    Cheers
    Chris
     
    ChrisH, Jul 12, 2006
    #3
    1. Advertisements

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. Don
    Replies:
    2
    Views:
    518
    Ralph Henneberg
    Dec 10, 2003
  2. Patrick LeBoutillier

    use/require handler?

    Patrick LeBoutillier, Jan 31, 2004, in forum: Perl
    Replies:
    4
    Views:
    594
    Gunnar Hjalmarsson
    Jan 31, 2004
  3. steffen staehle
    Replies:
    0
    Views:
    615
    steffen staehle
    Sep 7, 2004
  4. Joel Barsotti

    validator to require at least 4 characters?

    Joel Barsotti, Aug 11, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    365
    Joel Barsotti
    Aug 11, 2003
  5. Steve B.
    Replies:
    0
    Views:
    881
    Steve B.
    Feb 26, 2004
  6. =?Utf-8?B?RWFnbGVSZWRASGlnaEZseWluZ0JpcmRzLmNvbQ==

    Debugging ASP.NET Web Pages That Require SSL

    =?Utf-8?B?RWFnbGVSZWRASGlnaEZseWluZ0JpcmRzLmNvbQ==, Apr 30, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    8,014
    Nicole Calinoiu
    May 4, 2004
  7. clintonG
    Replies:
    4
    Views:
    368
    clintonG
    Jul 15, 2004
  8. Bennett Haselton
    Replies:
    4
    Views:
    7,757
    Frank Mamone
    Sep 10, 2004
Loading...