From: DrBenway Date: 2008-01-28T21:39:57+09:00 Subject: Re: to sleep no more On Jan 25, 9:54 pm, Judson Lester wrote: > On Jan 25, 2008 12:19 PM, DrBenway wrote: > > > > > Wow thnx Stephen for coocking up an example so fast :) > > That's indeed what I was planning to do. > > > That way I call snake = Animal.new(5, 10, "egg", "snake") once and > > from then on it leads a life on its own (that's what got me to think > > that I needed threads). > > You could do something like: > > > > class Animal > > > def initialize(i_period = 60, m_period = 120, from = "egg", to > > > = "chicken") > > > @incubation_period = i_period > > > @maturation_period = m_period > > > @slept = 0 > > > @from_type = from > > > @to_type = to > > > @current_type = @from_type > > > @children = [] > > Thread.run do > while true do > mysleep(1) > end > end > > > > end > > As a start, anyway. Much better to rejigger the whole thing so that > the thread looks like > > Thread.run do > sleep(incubation_period) > born > sleep(maturation_period) > mature > sleep(adult_lifetime) > die > end > > Also, it seems pretty obvious that you should do different species as > subclasses of Animal, so that: > > class Snake < Animal > def incubation_period; 5; end > > def maturation_period; 10; end > > def adult_lifetime; 15; end > > def born; @state="snake"; end > #etc, etc > end > > menagerie = [] > Snake.new(menagerie) > Deer.new(menagerie) > Fox.new(menagerie) > sleep 6 > menagerie.each {|animal| animal.give_birth} => "Snake 1: I had 150 > children. Deer 1: I had 1 child. Fox 1: I'm too young to reproduce" > > Granted, incomplete as it stands, but there's a direction there. > > -- > Your subnet is currently 169.254.0.0/16. You are likely to be eaten by a grue. This is indeed what I needed. Many thnx