First, I am likely doing something very wrong and simply
misunderstanding how class variables work. I'm a Perl guy by default so
this is my first big project in Ruby.
I'm trying to store another (unrelated) instantiated class in them so
that each time I instantiate a subclass I don't have to repeatedly
instantiate the unrelated classes. Not sure how else to say it, but
that certainly sounds like that sentence got away from me. Here is my
example (stripped down pseudo code so hopefully it still compiles..)
Here is what I WAS trying:
(it blows up)
class Importer
def initialize()
@@config = Config.new() if @@config.nil?
@@api = API.new() if @@api.nil?
@@util = Util.new() if @@util.nil?
@@db = DB.new() if @@db.nil?
@@dbh = @@db.dbh if @@dbh.nil?
end
def import()
raise "this is abstract"
end
end
Then I have multiple sub classes such as:
class ItemDataImporter < Importer
def import()
# do stuff
end
end
I've tried this:
(It works but instantiates Config/API/Util and DB for every subclass I
instantiate)
class Importer
@@config = nil
@@api = nil
@@util = nil
@@db = nil
@@dbh = nil
@@pull_id= nil
def initialize()
@@config = Config.new() if @@config.nil?
@@api = API.new() if @@api.nil?
@@util = Util.new() if @@util.nil?
@@db = DB.new() if @@db.nil?
@@dbh = @@db.dbh if @@dbh.nil?
end
def import()
raise "this is abstract"
end
end
I've tried this as well (also blows up)
class Importer
@@config = Config.new() if @@config.nil?
@@api = API.new() if @@api.nil?
@@util = Util.new() if @@util.nil?
@@db = DB.new() if @@db.nil?
@@dbh = @@db.dbh if @@dbh.nil?
def import()
raise "this is abstract"
end
end
Again, I'm certain it is just me either me misunderstanding the purpose
of class variables or misunderstanding something else entirely.
Any help would be mucho appreciated. Thanks in advance for fixing my
dumb.
···
--
Posted via http://www.ruby-forum.com/.