I’m relatively new to Ruby, and definitely to Modules. I’m working on
some database query tools, and wanted to break out database connections
and disconnects in separate methods for re-use elsewhere.
Unfortunately, when I use a method which connects to the database and
later use a method to disconnect it sees the variables as undefined
local variables.
This is just a very simple example to outline what I’m hoping to do. If
anyone has any ideas, please let me know. I’m likely way off base, but I
can’t seem to find any information on what I’m trying to do.
file1.rb:
module DB
Method to connect to the Database
def DB.connect
@@dbh = DBI.connect(‘DBI:OCI8:NAME’, ‘name’, ‘pw’)
end
Method to close the Database connection
def DB.disconnect
dbh.disconnect
end
end
file2.rb:
module RunAll
def RunAll.order
DB.connect
DB.disconnect
end
end
RunAll.order