Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'sqlite3'
- $main_path = '/some/path'
- $db_file = $main_path + "/file.db"
- class A
- @@db = SQLite3::Database.open('file.db')
- def self.some_other_method
- @@db.execute("whatever")
- end
- def some_method
- @@db.execute("whatever")
- end
- end
- class B
- def initialize
- init_db
- end
- def init_db
- Dir.mkdir($main_path) unless Dir.exists?($main_path)
- if File.exists?($db_file)
- db = SQLite3::Database.open($db_file)
- else
- db = SQLite3::Database.new($db_file)
- sql = <<SQL
- create table whatever (
- something varchar(30)
- );
- SQL
- db.execute(sql)
- end
- end
- B.new
- # in `initialize': unable to open database file (SQLite3::CantOpenException)
- # The line it's pointing to is 7 here.
- # So, the interpreter is running through the code and executing the class A class variable code first before getting to B.new which would initialize the DB. What is a good way to get around this?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement