I am trying to store an array of objects;
class ColumnMapping
@@column = nil
@@method = nil
def initialize(col, meth)
@@column = col
@@method = meth
end
def get_metadata_field()
return @@column
end
def get_method()
return @@method
end
end
class Massage
require 'thread'
@@column_mappings = []
@ba = nil
def initialize()
@@column_mappings << ColumnMapping.new("ESI Folder Path", "get_esi_folder_path")
@@column_mappings << ColumnMapping.new("Has Embedded Files", "get_has_embedded_items")
@ba = $utilities.getBulkAnnotatercolumn_mappings
end
end
When I debug, the data in @@column_mappings
are two instances of the second line;
@@column_mappings << ColumnMapping.new("Has Embedded Files", "get_has_embedded_items")
.
For some reason, it overwrites the array with the last inserted item, why?
For some reason, it overwrites the array with the last inserted item, why?
No, it doesn't. The items are separate/distinct. But the initializer for the second item mutates the "global" class variables, so all items look the same.
Don't use class variables (@@column
), use instance variables (@column
).
Free online book: Programming Ruby.