4

I am an experienced programmer learning Ruby (and liking it a lot). I'm working on setting up a database using SQLite3. In order to better learn Ruby, I'm tracing into SQLite3. What I don't understand is, where is the code for #new for the Database and Statement classes. Actually, I expect not a #new method, but a #initialize method.

SQLite3::Database.new(file, options = {})
SQLite3::Statement.new(db, sql)

The above two statements are from the documentation. But in my code when I try to trace into this

$db = SQLite3::Database.new"MyDBfile"

it just steps over.

Then later on when I try to trace into

#$db.execute 

I do get into the #execute method in the Database.rb file, but then it calls the #prepare method where I try to step into

stmt = SQLite3::Statement.new( self, sql )

but again no luck. It just steps over it.

I've scoured the source code, done searches etc but I cannot locate the initialize methods that are being called. Where are they ?

Thank you for considering this question.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Will
  • 337
  • 4
  • 15

1 Answers1

2

The initialize method for SQLite3::Database is implemented in C:

/* call-seq: SQLite3::Database.new(file, options = {})
 *
 * Create a new Database object that opens the given file. If utf16
 * is +true+, the filename is interpreted as a UTF-16 encoded string.
 *
 * By default, the new database will return result rows as arrays
 * (#results_as_hash) and has type translation disabled (#type_translation=).
 */
static VALUE initialize(int argc, VALUE *argv, VALUE self)

Similarly for SQLite3::Statement:

/* call-seq: SQLite3::Statement.new(db, sql)
 *
 * Create a new statement attached to the given Database instance, and which
 * encapsulates the given SQL text. If the text contains more than one
 * statement (i.e., separated by semicolons), then the #remainder property
 * will be set to the trailing text.
 */
static VALUE initialize(VALUE self, VALUE db, VALUE sql)

The Ruby debugger doesn't know how to step into C functions (assuming the SQLite3 extensions have even been compiled with debugging support) so it skips over them.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • Thank you for that. But it raises new questions. First, how does Ruby resolve a '#xxx.new' ? For example does it first look for a #initialize method in a Ruby file, and then when that fails it looks for the "C" code ? Second, how is the C code linked in ? Is it an obj file or library within the gem folder for SQLite3 ? – Will Mar 30 '12 at 17:59
  • ps. (I'm new to stackOverflow) - is my above comment appropriate? Or should it be a chat or even a separate question ? Thanks. (Working on my points !) – Will Mar 30 '12 at 18:03
  • @WilliamSmith: If you look at the bottom of the C files you'll see the `rb_define_method` calls that glue things together. The C files are compiled into library and dynamically loaded as needed. You should be able to find a C extension tutorial with a bit of googling. The comments are fine, most of us here are friendly (or at least pretend to be :). – mu is too short Mar 30 '12 at 18:25
  • Thanks for that reply. And in the meantime, I believe I've found some answers to my own question in the above comment within the Pragmatic Programmer's Guide, in the section called **Extending Ruby**: [http://www.ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html] - Kudos to stackOverflow, this is a great community and a very cool implementation. I hope to be able to become a contributor and not just a consumer. – Will Mar 30 '12 at 18:25