root / examples / pingpong / serial.rb
| 1 | #!/usr/bin/env ruby
|
|---|---|
| 2 | |
| 3 | class PingPong |
| 4 | |
| 5 | def initialize name |
| 6 | @name = name
|
| 7 | end
|
| 8 | |
| 9 | def pingpong count, partner |
| 10 | if count == 0 |
| 11 | puts "#{@name}: done"
|
| 12 | else
|
| 13 | if count % 500 == 0 or count % 500 == 1 |
| 14 | puts "#{@name}: pingpong #{count}"
|
| 15 | end
|
| 16 | partner.pingpong count-1, self |
| 17 | # sleep 0.001
|
| 18 | end
|
| 19 | end
|
| 20 | |
| 21 | end
|
| 22 | |
| 23 | ping = PingPong.new "ping" |
| 24 | pong = PingPong.new "pong" |
| 25 | |
| 26 | ping.pingpong ARGV[0].to_i, pong |