P
Peter Marks
I'd like to specify a quantity of object attributes when creating an
instance of an object. For example, I would like to create a Grid object
with 4 different 'row' attributes like this: Grid.new(4). Here's some
dirty code that does that:
class Grid
#Creates row1, row2, etc. depending on row arg
def initialize(rows)
row_count = 1
rows.times do
instance_eval "@row#{row_count} = 'xxx'"
row_count += 1
end
end
end
It's dirty, but it creates the attributes. I don't see how to
dynamically create accessor methods for all these attributes though. Any
suggestions? I sense there's a better way to do this.
instance of an object. For example, I would like to create a Grid object
with 4 different 'row' attributes like this: Grid.new(4). Here's some
dirty code that does that:
class Grid
#Creates row1, row2, etc. depending on row arg
def initialize(rows)
row_count = 1
rows.times do
instance_eval "@row#{row_count} = 'xxx'"
row_count += 1
end
end
end
It's dirty, but it creates the attributes. I don't see how to
dynamically create accessor methods for all these attributes though. Any
suggestions? I sense there's a better way to do this.