Grehom wrote:
> I have one line of code in a file called 'stuff.rb':
>      myhash = { "a" => "ay", "b" => "bee", "c" => "sea" }
> 
> and I wish to include it in another program called 'mainprog.rb' thus:
> 
>      require 'stuff'
> 
>      puts " a = " + myhash["a"]
> 
> when I run it I get an error message:
>     mainprog.rb:4: undefined local variable or method `myhash' for
> main:Object (NameError)
> 
> I guess I must be doing something dumb - I am basing this code on
> examples in 'Why's (poignant) guide to Ruby'.  I am using the very
> latest Windows version under XP Pro:
> C:\rubysrcs>ruby -v
> ruby 1.8.2 (2004-12-25) [i386-mswin32]
> 
> which I installed using the exe file: ruby182-15.exe
> 
> everything else I've tried seems to be working fine.  Thanks.

myhash is a local variable in stuff.rb and will thus not
be available when you #require the file. There are a few
options you have: you could either make it a constant
(MY_HASH), a global ($my_hash) or you could wrap it inside
a method and then call the method in your main file.


E

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