红宝石抛出错误为零

时间:2013-08-05 17:10:08

标签: ruby object null oci8

鉴于以下课程:

require 'rubygems'
require 'oci8'

class DB
  attr_reader :handle
  def initialize(username, password, db)
    @handle = OCI8.new(username,password,db)
    #We show an error if we don't have a handle after we try to connect to the DB
    raise ArgumentError, "Database connection failed" if @handle.nil
  end
end
def main()
  myHandle=DB.new('myUser','myPass','myDB')
  myHandle.handle().exec('select count(*) from vcas.deviceentitlement where rownum <= 100')
end

main()

我的脚本失败并显示错误:

`initialize': undefined method `nil' for #<OCI8:USER> (NoMethodError)
    from /home/ndefontenay/Ruby/purge_entitlement/entitlement.rb:20:in `new'
    from /home/ndefontenay/Ruby/purge_entitlement/entitlement.rb:20:in `main'
    from /home/ndefontenay/Ruby/purge_entitlement/entitlement.rb:24

我认为nil是检查对象是否已正确创建,但看起来它正在尝试运行一个名为nil的方法,该方法不存在。发生了什么事?

2 个答案:

答案 0 :(得分:1)

这是

@handle.nil?

llllllllllllllllllllll

看点?点表示接下来是方法调用。方法的名称拼写为nil?

答案 1 :(得分:0)

检查对象是否为nil的正确方法是nil?

在它们末尾带有问号的方法返回一个布尔值。