From: Eero Saynatkari Date: 2005-12-19T07:36:40+09:00 Subject: Re: Newbie: require 'filename' - undefined local variable or method... -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 2005.12.19 07:27, "Grehom" wrote: > Thanks Gene and Ezra, I tried your suggestions but with no luck. I > modified the main program 'mainprog.rb' as follows: > puts $: > load("stuff.rb", wrap=false) > puts myhash["a"] > > This produced following output: > C:\rubysrcs>ruby mainprog.rb > c:/ruby/lib/ruby/site_ruby/1.8 > c:/ruby/lib/ruby/site_ruby/1.8/i386-msvcrt > c:/ruby/lib/ruby/site_ruby > c:/ruby/lib/ruby/1.8 > c:/ruby/lib/ruby/1.8/i386-mswin32 > . > mainprog.rb:5: undefined local variable or method `myhash' for > main:Object (NameError) > > and it seems the pwd '.' is part of the Load Path already (without me > changing anything) > > So I'm still puzzled (there are a few examples of code like this in > aforementioned tutorial book)! I think there may be a problem with the gateway again.. The problem you have is that myhash is local to stuff.rb and will not be made available if you #require or #load the file. Instead, you have a few options: either make myhash a constant (MY_HASH), a global ($my_hash) or set up a method to retrieve it (def my_hash(); ...; end) and call the method from your main file. E -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux) iD8DBQFDpe0rxvA1l6h+MUMRAsKyAJ43oioZaeoEZ8OD4omB8r9rIjEh9gCfWwTB Lss8R5dkKElh589j7IT5RcE= =w3Tl -----END PGP SIGNATURE-----