I am playing around... I am a ruby newbie. I am encountering an error
I can't figure out. Probably something very silly I am doing wrong...
I have a class called "Artist" (I'll attach it below) that tries to
open an SQLite3 database and load a row into itself.
As I code I keep a "test.rb" around to try things out. In the test.rb
I require, open and execute a query successfully. I add a reference to
my class and try its load method and get this:
Mitchs-iMac-G5:~/Desktop/Projects/ArtManagerRuby mike$ ruby test.rb
1
Painting
Landscape
Oil
Canvas
Farmlands
This is a comment
3
Painting
Landscape
Watercolor
Paper
She kicks ASS!
Not only is her artwork the best, she is my wife!
4
Sketch
Landscape
Crayon
Paper
Loves the beach!
This guy can color with crayola crayons like nobody I have ever seen!
"stuff"
"done!"
"nope"
true
"yah baby!"
"this is a catenated string"
/opt/local/lib/ruby/vendor_ruby/1.8/sqlite3/database.rb:619:in
`const_get': uninitialized constant SQLite3::Driver::Native (NameError)
from
/opt/local/lib/ruby/vendor_ruby/1.8/sqlite3/database.rb:619:in
`load_driver'
from
/opt/local/lib/ruby/vendor_ruby/1.8/sqlite3/database.rb:616:in `each'
from
/opt/local/lib/ruby/vendor_ruby/1.8/sqlite3/database.rb:616:in
`load_driver'
from
/opt/local/lib/ruby/vendor_ruby/1.8/sqlite3/database.rb:107:in
`initialize'
from ./artist.rb:100:in `new'
from ./artist.rb:100:in `load'
from test.rb:41
-----------
(The output at first is the successful accessing of the database, the
error is when I call the load method of my class)
Here is the test.rb file:
require 'sqlite3'
require 'db_base'
require 'artist'
db = SQLite3::Database.new( DB_Base::DB_DATA )
db.execute( "select * from artists" ) do |row|
puts row
end
db.close
class Foo
def bar(gibberish=nil)
p gibberish
rescue
p 'Ppppffftt!'
ensure
p 'done!'
end
end
foo = Foo.new
foo.bar('stuff')
some_val = nil
if !some_val
p "nope"
else
p "yep!"
end
some_val = "y"
some_other_val = "y"
p (some_val == some_other_val)
p "yah baby!" if some_val == some_other_val
p 'this is a ' +
'catenated string'
a = Artist.new()
a.load(1)
puts a.to_s
-----------------
Here is the Artist class in artist.rb:
require 'sqlite3'
require 'db_base'
class Artist
# Define our class constants here. Class constants are
# immutable variables accessible by every instance of this
# class.
QRY_SELECT = 'select form, category, media, mediasupport,' +
'topic, comments from artists were id = ?'
QRY_INSERT = 'insert into artists(form, category, media,' +
'mediasupport, topic, comments) ' +
'values(?, ?, ?, ?, ?, ?)'
QRY_UPDATE = 'update artists set form = ?, category = ?, ' +
'media = ?, mediasupport = ?, topic = ?, comments = ? ' +
'where id = ?'
QRY_DELETE = 'delete from artists where id = ?'
# These are our accessors. They define our
# class data and their visibility. In this
# case we are declaring the ability of these
# items to be read.
attr_reader :id, :form, :category, :media
attr_reader :media_support, :topic, :comments
attr_accessor :dirty, :added
# Only the class itself can reference these.
protected :dirty, :added
# Our constructor takes all arguments. It also
# determines whether or not this item is new or
# existing.
def initialize(id=nil, form=nil, category=nil, media=nil,
media_support=nil, topic=nil, comments=nil)
@dirty = false
@added = false
@form = form
@category = category
@media = media
@media_support = media_support
@topic = topic
@comments = comments
@added = true if !id
end
# We manually program our writers (class values)
# because we need to check whether or not our values
# have actually changed. If they do, we mark our
# class object as "dirty".
def id=(id)
@dirty = true if id != @id
@id = id
end
def form=(form)
@dirty = true if form != @form
@form = form
end
def category=(category)
@dirty = true if category != @category
@category = category
end
def media=(media)
@dirty = true if media != @media
@media = media
end
def media_support=(media_support)
@dirty = true if media_support != @media_support
@media_support = media_support
end
def topic=(topic)
@dirty = true if topic != @topic
@topic = topic
end
def comments=(comments)
@dirty = true if comments != @comments
@comments = comments
end
# The rest of these are our methods for loading,
# saving and clearing out our object.
def load(id)
db = SQLite3::Database.new(DB_Base::DB_DATA)
db.execute(QRY_SELECT, id) do |row|
@form = row[0]
@category = row[1]
@media = row[2]
@media_support = row[3]
@topic = row[4]
@comments = row[5]
end
@id = id
@dirty = false
@added = false
db.close
end
def store
if @dirty or @added
db = SQLite3::Database.new(DB_Base::DB_DATA)
if @added
db.execute(QRY_INSERT,
@form, @category, @media, @media_support,
@topic, @comments )
@id = db.last_insert_row_id
@added = false
elsif @dirty
db.execute(QRY_UPDATE,
@form, @category, @media, @media_support,
@topic, @comments, @id )
@dirty = false
end
end
ensure
db.close unless db.closed?
end
def kill
db = SQLite3::Database.new(DB_Base::DB_DATA)
db.execute(QRY_DELETE, @id)
ensure
db.close unless db.closed?
end
def to_s
"Form : #{@form}\nMedia: #{@media}/#{@media_support}"
end
end
-----------------
This is db_base.rb...
module DB_Base
# Base database info. Keeps constants that identify
# our databases.
DB_DATA = '/Library/Application Support/ArtManager/ArtManager.db'
DB_IMAGES = '/Library/Application Support/ArtManager/Images.db'
end