[Leaplist] ruby stuff..

Fred Moore fred at fmeco.com
Fri Mar 9 03:17:42 EST 2007


Ok all of you ruby guys and gals..  It seems I don't understand something 
about variables and scope.. inside ruby..   Just started playing with it.. 

I have found several references that say that you don't need to define a 
variable.  they get defined the first time they are used..   Perhaps 
something happens differently to variable scope inside a block..  anyway I am 
confused.. perhaps someone can help me  out..  and educate this poor fool.. 3 
books don't seem to explain the problem..   google tells me to quit bothering 
it.  

the following code snipit runs just fine..   and outputs each line with a line 
number before it.. 

-----------------------------------------------------
#!/usr/bin/ruby -w
file = File.new("buildFile.tex", "r")  
counter=0
while (l = file.gets)
	puts "#{counter}:#{l}"
	counter +=1	
end
file.close
-----------------------------------------------------

this next snipit blows up with a 

"undefined local variable or method `all'  for main:Object (Name Error)"

all that is added is line 5 all[counte]=l, I want to spin an array of lines 
from the file.. 

----------------------------------------------------
#!/usr/bin/ruby -w
file = File.new("buildFile.tex", "r")  
counter=0
while (l = file.gets)
	all[counter]=l
	puts "#{counter}:#{l}"
	counter +=1	
end
file.close

---------------------------------------------------

 so who can explain this behavior.. Fred

-- 
"everyone has a right to his own opinion, but not his own facts"
Fred/WD8KNI


More information about the Leaplist mailing list