From: Jim Weirich Date: 2005-01-20T20:56:31+09:00 Subject: Re: Confused about variable "declarations" On Thursday 20 January 2005 06:41 am, Graham Nicholls wrote: > This was my understanding, but I keep seeing this sort of thing: > > ../duprop:136:in `find_pcap_offset': undefined local variable or method > `offset' for # (NameError) [...] > pcap_file.each_line() do |line| > if line.match(@printer_name) > print("Found printer #{@printer_name} in [#{line}]\n") if $debug > found=true > offset=line.split()[1].to_i > break [...] You didn't show the complete listing, so I am guessing here. offset does indeed spring into existence at the point you assign it. Unfortunately, it only exists for the duration of the block that begins with the line "pcap_file.each_line() do |line|". I suspect that the error message you were getting occured when you tried to access offset later in the code, outside of the block. Assigning to offset outside the block means that it existed before the block was entered, and will continue to exist after the block is done. This is one of the little oddities that Matz intends to fix in Ruby 2.0. -- -- Jim Weirich jim@weirichhouse.org http://onestepback.org ----------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)