to sleep no more

D

DrBenway

hi all,

I'm a ruby noop trying his first steps. I'd like a little advice.

What I want to make is the following.

An object Chicken
- its attribute starts as 'egg'
- after x times sleep() it the attribute gets the value chicken
- x times sleep later the object can make a call to make a new
instance of chicken (creating another egg)

I could make a method to change the attribute and call this from my
main program.
In other words my sleep method would be in my main program and call
the objects method.
I was wondering if it would be possible to keep the sleep in the
object.
(Since I was planning on creating some other animals as well, each
with a special amount of time needed to go to the next step.)

I think it's called threads in ruby but I'm not sure. (if it's called
threads is this the way to handle this problem? Does it act like an
Ajax call?)

Many thnx

DrBenway
 
S

Stephen Kratzer

hi all,

I'm a ruby noop trying his first steps. I'd like a little advice.

What I want to make is the following.

An object Chicken
- its attribute starts as 'egg'
- after x times sleep() it the attribute gets the value chicken
- x times sleep later the object can make a call to make a new
instance of chicken (creating another egg)

I could make a method to change the attribute and call this from my
main program.
In other words my sleep method would be in my main program and call
the objects method.
I was wondering if it would be possible to keep the sleep in the
object.
(Since I was planning on creating some other animals as well, each
with a special amount of time needed to go to the next step.)

I think it's called threads in ruby but I'm not sure. (if it's called
threads is this the way to handle this problem? Does it act like an
Ajax call?)

Many thnx

DrBenway

Something like this might work for your purposes:

class Animal
attr_reader :incubation_period, :maturation_period, :from_type,
:to_type, :current_type, :slept, :children

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 = []
end

def mysleep(seconds = @incubation_period)
@slept += sleep(seconds)
if @slept >= @incubation_period
@current_type = @to_type
end
end

def give_birth(count = 1)
if @slept >= @maturation_period
for number in children.length..(children.length +
count - 1)
puts "I had another child."
children[number] =
Animal.new(@incubation_period, @maturation_period, @from_type, @to_type);
end
else
puts "I'm too young to give birth."
end
end
end

snake = Animal.new(5, 10, "egg", "snake")
snake.mysleep(4)
puts snake.current_type
snake.give_birth
snake.mysleep(4)
puts snake.current_type
snake.give_birth
snake.mysleep(4)
puts snake.current_type
snake.give_birth(5)
 
D

DrBenway

I'm a ruby noop trying his first steps. I'd like a little advice.
What I want to make is the following.
An object Chicken
- its attribute starts as 'egg'
- after x times sleep() it the attribute gets the value chicken
- x times sleep later the object can make a call to make a new
instance of chicken (creating another egg)
I could make a method to change the attribute and call this from my
main program.
In other words my sleep method would be in my main program and call
the objects method.
I was wondering if it would be possible to keep the sleep in the
object.
(Since I was planning on creating some other animals as well, each
with a special amount of time needed to go to the next step.)
I think it's called threads in ruby but I'm not sure. (if it's called
threads is this the way to handle this problem? Does it act like an
Ajax call?)
Many thnx

Something like this might work for your purposes:

class Animal
attr_reader :incubation_period, :maturation_period, :from_type,
:to_type, :current_type, :slept, :children

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 = []
end

def mysleep(seconds = @incubation_period)
@slept += sleep(seconds)
if @slept >= @incubation_period
@current_type = @to_type
end
end

def give_birth(count = 1)
if @slept >= @maturation_period
for number in children.length..(children.length +
count - 1)
puts "I had another child."
children[number] =
Animal.new(@incubation_period, @maturation_period, @from_type, @to_type);
end
else
puts "I'm too young to give birth."
end
end
end

snake = Animal.new(5, 10, "egg", "snake")
snake.mysleep(4)
puts snake.current_type
snake.give_birth
snake.mysleep(4)
puts snake.current_type
snake.give_birth
snake.mysleep(4)
puts snake.current_type
snake.give_birth(5)

Wow thnx Stephen for coocking up an example so fast :)
That's indeed what I was planning to do.

The only thing that may not have been very obvious (sorry for this) in
my example above was:
Is there a way to take the snake.mysleep(4) out of our main program
and put it in the class/object itself
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).
Same goes for all the other snakes that get born from the first.
I don't feel like messing with arrays of
snakes,pigs,boars,chickens,... out of the classes
 
J

Judson Lester

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
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.
 
S

Stephen Kratzer

hi all,

I'm a ruby noop trying his first steps. I'd like a little advice.

What I want to make is the following.

An object Chicken
- its attribute starts as 'egg'
- after x times sleep() it the attribute gets the value chicken
- x times sleep later the object can make a call to make a new
instance of chicken (creating another egg)

I could make a method to change the attribute and call this from my
main program.
In other words my sleep method would be in my main program and call
the objects method.
I was wondering if it would be possible to keep the sleep in the
object.
(Since I was planning on creating some other animals as well, each
with a special amount of time needed to go to the next step.)

I think it's called threads in ruby but I'm not sure. (if it's called
threads is this the way to handle this problem? Does it act like an
Ajax call?)

Many thnx

DrBenway

Something like this might work for your purposes:

class Animal
attr_reader :incubation_period, :maturation_period, :from_type,

:to_type, :current_type, :slept, :children

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 = []
end

def mysleep(seconds = @incubation_period)
@slept += sleep(seconds)
if @slept >= @incubation_period
@current_type = @to_type
end
end

def give_birth(count = 1)
if @slept >= @maturation_period
for number in children.length..(children.length +
count - 1)
puts "I had another child."
children[number] =
Animal.new(@incubation_period, @maturation_period, @from_type, @to_type);
end
else
puts "I'm too young to give birth."
end
end
end

snake = Animal.new(5, 10, "egg", "snake")
snake.mysleep(4)
puts snake.current_type
snake.give_birth
snake.mysleep(4)
puts snake.current_type
snake.give_birth
snake.mysleep(4)
puts snake.current_type
snake.give_birth(5)

Wow thnx Stephen for coocking up an example so fast :)
That's indeed what I was planning to do.

The only thing that may not have been very obvious (sorry for this) in
my example above was:
Is there a way to take the snake.mysleep(4) out of our main program
and put it in the class/object itself
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).
Same goes for all the other snakes that get born from the first.
I don't feel like messing with arrays of
snakes,pigs,boars,chickens,... out of the classes

Ah, now I understand. Yes, if you want each Animal to be able to run through
its lifecycle independently and concurrently, you'd need to use threads. The
example class above would be simplified by the fact that each Animal would be
responsible for its own lifecycle. The constructor would just need to sleep
for the incubation period, update its current type, sleep for the maturation
period minus the incubation period (if any), give birth (spawning other
threads and creating other Animals), and then die at some point. You can find
some info on using threads in Ruby here:
http://www.ruby-doc.org/docs/ProgrammingRuby/

Stephen Kratzer
Network Engineer
CTI Networks, Inc.
 
P

Philipp Hofmann

hi,

ok let's code a zoo. threads is what you are looking for, but there
might be a better way. but threads first.

class Animal

def initialize
@lifetime_in_sec = 0
@dead = false
thread = Thread.new do
until @dead
@lifetime_in_sec += 1
sleep 1
end
end
end

def kill
@dead = true
end

end

depending on your zoo this might lead to a lot of threads, so maybe
you should think about using a more event-based design. (make the egg
by initialization register a 'timed' event to become a biddy.)

shutdown = true
$events = Hash.new { |hash, key| hash[key] = [] }
$time = 0

class Animal
def initialize
$events[$time+time_to_become_a_biddy] << self
end
def progress
# become a biddy or chicken or ...
$events[$time+time_to_become_a_chicken] << self if @state==:biddy
end
end

event_handler = Thread.new do
counter = 0
until shutdown
counter += 1
$events[counter].each { |animal| animal.progress }
end
end

this way your simulation is calucaluatet as fast as possible and you
can throttle it to be realtime. asuming your machine is fast enough
for your zoo. ;)

g phil


I'm a ruby noop trying his first steps. I'd like a little advice.
What I want to make is the following.
An object Chicken
- its attribute starts as 'egg'
- after x times sleep() it the attribute gets the value chicken
- x times sleep later the object can make a call to make a new
instance of chicken (creating another egg)
I could make a method to change the attribute and call this from my
main program.
In other words my sleep method would be in my main program and call
the objects method.
I was wondering if it would be possible to keep the sleep in the
object.
(Since I was planning on creating some other animals as well, each
with a special amount of time needed to go to the next step.)
I think it's called threads in ruby but I'm not sure. (if it's called
threads is this the way to handle this problem? Does it act like an
Ajax call?)
Many thnx

Something like this might work for your purposes:

class Animal
attr_reader :incubation_period, :maturation_period, :from_type,
:to_type, :current_type, :slept, :children

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 = []
end

def mysleep(seconds = @incubation_period)
@slept += sleep(seconds)
if @slept >= @incubation_period
@current_type = @to_type
end
end

def give_birth(count = 1)
if @slept >= @maturation_period
for number in children.length..(children.length +
count - 1)
puts "I had another child."
children[number] =
Animal.new(@incubation_period, @maturation_period, @from_type, @to_type);
end
else
puts "I'm too young to give birth."
end
end
end

snake = Animal.new(5, 10, "egg", "snake")
snake.mysleep(4)
puts snake.current_type
snake.give_birth
snake.mysleep(4)
puts snake.current_type
snake.give_birth
snake.mysleep(4)
puts snake.current_type
snake.give_birth(5)

Wow thnx Stephen for coocking up an example so fast :)
That's indeed what I was planning to do.

The only thing that may not have been very obvious (sorry for this) in
my example above was:
Is there a way to take the snake.mysleep(4) out of our main program
and put it in the class/object itself
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).
Same goes for all the other snakes that get born from the first.
I don't feel like messing with arrays of
snakes,pigs,boars,chickens,... out of the classes
 
D

DrBenway

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

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.

This is indeed what I needed. Many thnx
 
D

DrBenway

hi,

ok let's code a zoo. threads is what you are looking for, but there
might be a better way. but threads first.

class Animal

def initialize
@lifetime_in_sec = 0
@dead = false
thread = Thread.new do
until @dead
@lifetime_in_sec += 1
sleep 1
end
end
end

def kill
@dead = true
end

end

depending on your zoo this might lead to a lot of threads, so maybe
you should think about using a more event-based design. (make the egg
by initialization register a 'timed' event to become a biddy.)

shutdown = true
$events = Hash.new { |hash, key| hash[key] = [] }
$time = 0

class Animal
def initialize
$events[$time+time_to_become_a_biddy] << self
end
def progress
# become a biddy or chicken or ...
$events[$time+time_to_become_a_chicken] << self if @state==:biddy
end
end

event_handler = Thread.new do
counter = 0
until shutdown
counter += 1
$events[counter].each { |animal| animal.progress }
end
end

this way your simulation is calucaluatet as fast as possible and you
can throttle it to be realtime. asuming your machine is fast enough
for your zoo. ;)

g phil

On Friday 25 January 2008 13:25:00 DrBenway wrote:
hi all,
I'm a ruby noop trying his first steps. I'd like a little advice.
What I want to make is the following.
An object Chicken
- its attribute starts as 'egg'
- after x times sleep() it the attribute gets the value chicken
- x times sleep later the object can make a call to make a new
instance of chicken (creating another egg)
I could make a method to change the attribute and call this from my
main program.
In other words my sleep method would be in my main program and call
the objects method.
I was wondering if it would be possible to keep the sleep in the
object.
(Since I was planning on creating some other animals as well, each
with a special amount of time needed to go to the next step.)
I think it's called threads in ruby but I'm not sure. (if it's called
threads is this the way to handle this problem? Does it act like an
Ajax call?)
Many thnx
DrBenway
Something like this might work for your purposes:
class Animal
attr_reader :incubation_period, :maturation_period, :from_type,
:to_type, :current_type, :slept, :children
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 = []
end
def mysleep(seconds = @incubation_period)
@slept += sleep(seconds)
if @slept >= @incubation_period
@current_type = @to_type
end
end
def give_birth(count = 1)
if @slept >= @maturation_period
for number in children.length..(children.length +
count - 1)
puts "I had another child."
children[number] =
Animal.new(@incubation_period, @maturation_period, @from_type, @to_type);
end
else
puts "I'm too young to give birth."
end
end
end
snake = Animal.new(5, 10, "egg", "snake")
snake.mysleep(4)
puts snake.current_type
snake.give_birth
snake.mysleep(4)
puts snake.current_type
snake.give_birth
snake.mysleep(4)
puts snake.current_type
snake.give_birth(5)
Wow thnx Stephen for coocking up an example so fast :)
That's indeed what I was planning to do.
The only thing that may not have been very obvious (sorry for this) in
my example above was:
Is there a way to take the snake.mysleep(4) out of our main program
and put it in the class/object itself
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).
Same goes for all the other snakes that get born from the first.
I don't feel like messing with arrays of
snakes,pigs,boars,chickens,... out of the classes

Thnx for the example Philipp.
The first one I get, but the second makes my head spin ^_^
To much of a Ruby/OOP noob I guess :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top