ruby - Is there a more efficient way of ensuring my database gets closed? -
i'm using daybreak library, key/value store.
i open perform operation this:
db = daybreak::db.new $showdatabasename
then whatever want, , close this:
db.close
it seems wasted effort everywhere want use however. seems might more efficient declare class variable in initialize this:
def initialize @db = daybreak::db.new $showdatabasename end
it means rest of class can use without initializing , closing each time, message reading:
daybreak database not closed, state might inconsistent
is there better way of doing this, deals both repetition , warning?
use block:
def use_db(database_name) db = daybreak::db.new(database_name) yield db ensure db.close end use_db($showdatabasename) |db| # db end
Comments
Post a Comment