0

次のクラスがあるとします。

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。どうしたの?

4

2 に答える 2

1

これは

@handle.nil?

llllllllllllllllllllll

ドットが見えますか?ドットは、次がメソッド呼び出しであることを意味します。メソッド名のスペルはnil?.

于 2013-08-05T17:17:31.680 に答える