4

我是一位经验丰富的程序员,正在学习 Ruby(并且非常喜欢它)。我正在使用 SQLite3 设置数据库。为了更好地学习 Ruby,我正在追踪 SQLite3。我不明白的是,数据库和语句类的#new 代码在哪里。实际上,我期望的不是#new 方法,而是#initialize 方法。

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

以上两个陈述来自文档。但是在我的代码中,当我尝试追踪这个时

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

它只是走了过去。

然后稍后当我尝试追踪

#$db.execute 

我确实进入了 Database.rb 文件中的#execute 方法,但随后它调用了我尝试进入的#prepare 方法

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

但再次没有运气。它只是跨过它。

我已经搜索了源代码,完成了搜索等,但我找不到正在调用的初始化方法。他们在哪 ?

感谢您考虑这个问题。

4

1 回答 1

2

initialize方法SQLite3::Database在 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)

同样对于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)

Ruby 调试器不知道如何单步执行 C 函数(假设 SQLite3 扩展甚至已经编译了调试支持),因此它会跳过它们。

于 2012-03-30T06:25:56.723 回答