From: "Carlo E. Prelz" Date: 2013-02-14T15:27:06+09:00 Subject: Re: Ruby Multithreaded producer-consumer problem Subject: Ruby Multithreaded producer-consumer problem Date: gio 14 feb 13 12:50:48 +0900 Quoting Abhijit Sarkar (lists@ruby-forum.com): > 1) The attached code gives me the following exception: > /Users/Abhijit/Workspace/eclipse/ruby/learn_to_program/orange_tree.rb:84: > warning: instance variable @orange_tree not initialized > /Users/Abhijit/Workspace/eclipse/ruby/learn_to_program/orange_tree.rb:84:in > `': undefined method `age' for nil:NilClass > (NoMethodError) > from > /Users/Abhijit/Workspace/eclipse/ruby/learn_to_program/orange_tree.rb:45:in > `
' Instance variables are only accessible from within methods. Lines 83-86 are not within a method. Take lines 83-86 out of the class definition, and change them to something like: worker=Worker.new(Mutex.new,ConditionVariable.new,OrangeTree.new) worker.do_some_work until(worker.orange_tree.age==OrangeTree::AGE_TO_DIE) # wait for the Threads to finish end Of course, variable orange_tree has to be visible from the outside, so you have to add something like attr_reader(:orange_tree) at the beginning of the Worker class. This way you should not have those exceptions anymore. Carlo -- * Se la Strada e la sua Virtu' non fossero state messe da parte, * K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)