0

Given the following class:

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()

my script fails with error:

`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

I thought nil was to check whether an object has been created properly, but it looks like it's trying to run a method called nil which doesn't exist. What's going on?

sawa
  • 165,429
  • 45
  • 277
  • 381
Nicolas de Fontenay
  • 2,170
  • 5
  • 26
  • 51
  • You have not written which file is `entitlement.rb`. And, since you are not showing the entire code, put the line numbers to the code so that it matches the description in the backtrace. – sawa Aug 05 '13 at 17:21
  • And, do you think or do you not think that a method called `nil` exists? It sounds like both, which is contradiction. – sawa Aug 05 '13 at 17:23

2 Answers2

1

It's

@handle.nil?

llllllllllllllllllllll

See the dot? The dot means that what follows is a method call. The method's name is spelled nil?.

7stud
  • 46,922
  • 14
  • 101
  • 127
0

The proper method to check whether an object is nil is nil?

Methods with a question mark at the end of them return a boolean.