Hello again!
I've refactored a bit the code of Wolfgang. Now, it do a lot of reuse through
inheretance. I'm really curious to know what this refactored code would give
under ruby 1.9.0. Would you mind to test it for me, please?
Here it is:
class Animal
@@born = 0
def initialize
@@born += 1
self.class.anew
end
def self.anew
puts "a new animal"
end
def self.born
@@born
end
end
class Dog<Animal
def self.anew
puts "a new dog"
end
end
class Cat<Animal
def self.anew
puts "a new cat"
end
end
print "#{Cat.born} cats, #{Dog.born} dogs, #{Animal.born} animals\n"
2.times{Dog.new}
print "#{Cat.born} cats, #{Dog.born} dogs, #{Animal.born} animals\n"
3.times{Cat.new}
print "#{Cat.born} cats, #{Dog.born} dogs, #{Animal.born} animals\n"
It still gives the same output:
0 cats, 0 dogs, 0 animals
a new dog
a new dog
2 cats, 2 dogs, 2 animals
a new cat
a new cat
a new cat
5 cats, 5 dogs, 5 animals
Thanks in advance,
Lionel Thiry
ts wrote:
>>>>>>"W" == Wolfgang NáÅasi-Donner <wonado / donnerweb.de> writes:
>
>
>>>>>Output >>>
>
> W> 0 cats, 0 dogs, 0 animals
> W> a new dog
> W> a new dog
> W> 2 cats, 2 dogs, 2 animals
> W> a new cat
> W> a new cat
> W> a new cat
> W> 5 cats, 5 dogs, 5 animals
>
>>>>>End of Example >>>
>
>
> With this version of ruby
>
> uln% ruby -v
> ruby 1.9.0 (2005-03-14) [x86_64-linux]
> uln%
>
> The result is
>
> 0 cats, 0 dogs, 0 animals
> a new dog
> a new dog
> 0 cats, 2 dogs, 0 animals
> a new cat
> a new cat
> a new cat
> 3 cats, 2 dogs, 0 animals
>
>