Advertisement
Guest User

Stuck.

a guest
Sep 6th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.95 KB | None | 0 0
  1. require 'sqlite3'
  2.  
  3. $main_path = '/some/path'
  4. $db_file = $main_path + "/file.db"
  5.  
  6. class A
  7.   @@db = SQLite3::Database.open('file.db')
  8.  
  9.   def self.some_other_method
  10.     @@db.execute("whatever")
  11.   end
  12.  
  13.   def some_method
  14.     @@db.execute("whatever")
  15.   end
  16. end
  17.  
  18. class B
  19.   def initialize
  20.     init_db
  21.   end
  22.  
  23.   def init_db
  24.     Dir.mkdir($main_path) unless Dir.exists?($main_path)
  25.     if File.exists?($db_file)
  26.       db = SQLite3::Database.open($db_file)
  27.     else
  28.       db = SQLite3::Database.new($db_file)
  29.       sql = <<SQL
  30. create table whatever (
  31.   something varchar(30)
  32. );
  33. SQL
  34.     db.execute(sql)
  35.   end
  36. end
  37.  
  38. B.new
  39.  
  40.  
  41. # in `initialize': unable to open database file (SQLite3::CantOpenException)
  42. # The line it's pointing to is 7 here.
  43. # 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