I started on Ruby less than a week ago but have already come to appreciate the power of the language. I am trying my hands on a classic producer-consumer problem, implemented as an Orange tree (c.f. http://pine.fm/LearnToProgram/?Chapter=09). The Orange tree grows each year until it dies and produces a random number of Oranges each year (Producer). Oranges can be picked as long there are any on the tree (Consumer).
I've got two problems here:
The following code gives me the following exception (can't attach, no option):
/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 `'
I am not sure that the multithreading part is correctly coded.
I've got myself a couple of books, including "Programming Ruby" and "The Ruby Programming Language", but none of them contain a true "producer-consumer problem".
P.S: For the sake of full disclosure, I've also posted this question in the Ruby forum. However, I have seen excellent answers and/or suggestions provided here and hope that I'd get some of those too.
require 'thread'
class OrangeTree
GROWTH_PER_YEAR = 1
AGE_TO_START_PRODUCING_ORANGE = 3
AGE_TO_DIE = 7
ORANGE_COUNT_RELATIVE_TO_AGE = 50
def initialize
@height = 0
@age = 0
@orange_count = 0
end
def height
return @height
end
def age
return @age
end
def count_the_oranges
return @orange_count
end
def one_year_passes
@age += 1
@height += GROWTH_PER_YEAR
@orange_count = Math.rand(@age..AGE_TO_DIE) * Math.log(@age) * ORANGE_COUNT_RELATIVE_TO_AGE
end
def pick_an_orange
if (@age == AGE_TO_DIE)
puts "Sorry, the Orange tree is dead"
elsif (@orange_count > 0)
@orange_count -= 1
puts "The Orange is delicious"
else
puts "Sorry, no Oranges to pick"
end
end
end
class Worker
def initialize(mutex, cv, orange_tree)
@mutex = mutex
@cv = cv
@orange_tree = orange_tree
end
def do_some_work
Thread.new do
until (@orange_tree.age == OrangeTree.AGE_TO_DIE)
@mutex.synchronize do
sleep_time = rand(0..5)
puts "Orange picker going to sleep for #{sleep_time}"
sleep(sleep_time)
puts "Orange picker woke up after sleeping for #{sleep_time}"
@orange_tree.pick_an_orange
puts "Orange picker waiting patiently..."
@cv.wait(@mutex)
end
end
end
Thread.new do
until (@orange_tree.age == OrangeTree.AGE_TO_DIE)
@mutex.synchronize do
sleep_time = rand(0..5)
puts "Age increaser going to sleep for #{sleep_time}"
sleep(sleep_time)
puts "Age increaser woke up after sleeping for #{sleep_time}"
@orange_tree.one_year_passes
puts "Age increaser increased the age"
@cv.signal
end
end
end
end
Worker.new(Mutex.new, ConditionVariable.new, OrangeTree.new).do_some_work
until (@orange_tree.age == OrangeTree.AGE_TO_DIE)
# wait for the Threads to finish
end
end