>>>>> "T" == Thomas Junier <tjunier / pcisrec-d402b.unil.ch> writes:

 add this line, perhaps you'll better understand what ruby do

T> [0,1].each {|n|
T> 	print "n: #{n}\n"
T> 	case n
T> 		when 0
T> 			a = 0
T> 			print "a: #{a}\n"	# -> 0
T> 			# never heard of b
T> 		when 1
T> 			b = 1

                        print "a : ", a, "\n"

T> 			print "a: #{a}\n"	# Name Error
T> 			print "b: #{b}\n"
T> 	end
T> }

 There are 2 steps : compile phase and runtime phase

 At compile time, the variable a and b are resolved as local variable to
 the block, because ruby has seen the first occurence of these variable in
 the block.

 But this is valid *only* for the variable that ruby can see (at compile
 time) and *NOT* for variables which are in a string like in "a: #{a}\n"

 At runtime, each time that ruby enter in the block, it reset the list
 of local variables for this block (i.e. you have an empty list)

 When it find :

		print "a: #{a}\n"	# Name Error

 it will the first time compile the part between #{}. Because for this
iteration a was never assigned, it don't exist and ruby resolve it as a
function call. This is why it give an error.


Guy Decoux