This is a pretty simple question.
def func1
t2=Thread
while true
# if t2.alive?
# puts "GUI is running"
# end
puts Thread.t2.stop?
puts "func1 at: #{Time.now}"
sleep(1)
end
end
t1=Thread.new{func1()}
t2=Thread.new{TheGUI()}
t1.join
t2.join
t2 is only declared later on in the code, so I am getting errors when trying to run this.
The error is ‘undefined local variable or method `t2”
How can I fix this without reordering my code?
Thanks
Your snippet is pretty small, so it’s hard to tell if your code is at top-level or in a class.
If
t2is supposed to be a global variable, note that Ruby prefixes global variables with a$:$t2.If
t2is supposed to be a class member, note that Ruby prefixes member variables with a@:@t2.Update
Your updated code is making an alias for the
Threadclass namedt2. Check this output:Furthermore, that
t2alias is only in force in the scope of thefunc1function definition.The simplest way to amend your code is probably to change
func1to take a parameter: