The below uses pseudo-random number generation to populate an array
with various monsters, all inherit from Creature. Can a list of
classes which inherit from Creature be generated automagically? There
must be a better technique to fill the array then the case
statement...
puts "\nquantity of creatures:"
numOfCreatures = gets.chomp.to_i
someCreatures = ArrayOfCreatures.new
0.upto(numOfCreatures) do |i|
creatureType = Kernel.rand(4)
case creatureType
when 0
someCreatures[i]=AssistantViceTentacleAndOmbudsman.new
when 1
someCreatures[i]=Dragon.new
when 2
someCreatures[i]=DwarvenAngel.new
when 3
someCreatures[i]=TeethDeer.new
end
end
The below uses pseudo-random number generation to populate an array
with various monsters, all inherit from Creature. Can a list of
classes which inherit from Creature be generated automagically? There
must be a better technique to fill the array then the case
statement...
puts "\nquantity of creatures:"
numOfCreatures = gets.chomp.to_i
someCreatures = ArrayOfCreatures.new
0.upto(numOfCreatures) do |i|
creatureType = Kernel.rand(4)
case creatureType
when 0
someCreatures[i]=AssistantViceTentacleAndOmbudsman.new
when 1
someCreatures[i]=Dragon.new
when 2
someCreatures[i]=DwarvenAngel.new
when 3
someCreatures[i]=TeethDeer.new
end
end
puts "\nquantity of creatures:"
num_of_creatures = gets.to_i
num_of_creatures.times do |i|
creatures[i] = creature_classes[rand(4)].new
end
David
···
On Thu, 8 Nov 2007, Thufir wrote:
--
Upcoming training by David A. Black/Ruby Power and Light, LLC:
* Advancing With Rails, Edison, NJ, November 6-9
* Advancing With Rails, Berlin, Germany, November 19-22
* Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.rubypal.com for details!
The below uses pseudo-random number generation to populate an array
with various monsters, all inherit from Creature. Can a list of
classes which inherit from Creature be generated automagically?
Perhaps:
class Creature @creature_classes =
# hook to note when a class inherits
def self.inherited(klass) @creature_classes << klass
end
# generate a random creature from among known subclasses
def self.random_creature @creature_classes[ rand(@creature_classes.length) ].new
end
end
The below uses pseudo-random number generation to populate an array
with various monsters, all inherit from Creature. Can a list of
classes which inherit from Creature be generated automagically? There
must be a better technique to fill the array then the case
statement...
--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama
The below uses pseudo-random number generation to populate an array
with various monsters, all inherit from Creature. Can a list of
classes which inherit from Creature be generated automagically? There
must be a better technique to fill the array then the case
statement...
--
use.inject do |as, often| as.you_can - without end
Aha, I've been trying to figure out what a "hook" meant!
thanks,
Thufir
···
On Nov 7, 4:15 pm, Alex Fenton <a...@deleteme.pressure.to> wrote:
Thufir wrote:
> The below uses pseudo-random number generation to populate an array
> with various monsters, all inherit from Creature. Can a list of
> classes which inherit from Creature be generated automagically?
Perhaps:
class Creature @creature_classes =
# hook to note when a class inherits
def self.inherited(klass) @creature_classes << klass
end
As an off-topic aside, I really like what you've done here. Normally I
do things like this:
class Foo @all =
class << self
attr_reader :all
end
def initialize
self.class.all << self
end
end
...but I've now seen the light how a constant scoped to the class can
make life far simpler.
class Foo
ALL =
def initialize
ALL << self
end
end
There's even some interesting benefits to the scoping, like:
# By inheriting the superclass's initialize,
# Bar1 instances get put into Foo::ALL
class Bar1 < Foo; end
# But I can totally branch on my own without worrying about
# any class variable sort of nonsense
class Bar2 < Foo
ALL = # separate from Foo::ALL
def initialize
ALL << self
end
end
Thanks for that, Ara!
···
On Nov 7, 5:16 pm, "ara.t.howard" <ara.t.how...@gmail.com> wrote:
creatures = ArrayOfCreatures.new
[...]
num_of_creatures.times do |i|
creatures[i] = creature_classes[rand(4)].new
end
Assuming that ArrayOfCreatures.new works like Array.new this can be simplified
to:
creatures = ArrayOfCreatures.new(num_of_creatures) do |i|
creature_classes[rand(4)].new
end
class Foo
ALL =
def initialize
ALL << self
end
end
There's even some interesting benefits to the scoping, like:
# By inheriting the superclass's initialize,
# Bar1 instances get put into Foo::ALL
class Bar1 < Foo; end
# But I can totally branch on my own without worrying about
# any class variable sort of nonsense
class Bar2 < Foo
ALL = # separate from Foo::ALL
def initialize
ALL << self
end
end
Assuming that ArrayOfCreatures.new works like Array.new this can be simplified
to:
creatures = ArrayOfCreatures.new(num_of_creatures) do |i|
creature_classes[rand(4)].new
end
It took me a few reads to appreciate, but I like that alot