From: Ryan Leavengood Date: 2005-06-27T07:39:27+09:00 Subject: Re: newbie scope question Charles L. Snyder wrote: > > Why does this not work? Because the variable doc is local to the top-level scope and can't be seen inside the methods of Crap. This works: $doc = File::open("my.txt") class Crap def junko @doc = $doc @doc.each {|line| print line} end end myobj=Crap.new myobj.junko $doc.close $doc is now a global. Of course, I would not recommend coding this way. For example your original code never closed the file (which is probably not that bad here because at exit open files will be closed.) In addition globals are bad in most cases. Using File::open with a block, and passing in the file (like your second example) is better: class Crap def junko(doc) @doc = doc @doc.each {|line| print line} end end File::open("my.txt") do |file| myobj=Crap.new myobj.junko(file) end That way the file is closed automatically when the block ends. Ryan