2013-08-05 53 views
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。這是怎麼回事?

+0

你還沒有寫過哪個文件是'entitlement.rb'。而且,由於您沒有顯示完整的代碼,請將行號放入代碼中,以便它與回溯中的描述相匹配。 – sawa

+0

而且,您是否認爲或不認爲存在一種名爲'nil'的方法?這聽起來像兩個,這是矛盾的。 – sawa

回答

1

這是

@handle.nil?

llllllllllllllllllllll

見點?點意味着接下來是一個方法調用。該方法的名稱拼寫爲nil?

+0

非常感謝。我完全錯過了問號。 剛開始學習ruby,很多事情都要去了。 很多事情仍然令我困惑。 –

+0

@ndefontenay,不客氣。 – 7stud

0

適當的方法來檢查一個對象是否是nil是用問號nil?

方法在它們的端部返回一個布爾值。