raise "NullPointerException" in Ruby

java, ruby

Solution

Raising an ArgumentError could suit your situation. You could do something like this:

def i_dont_take_nils_from_nobody x
  raise ArgumentError.new "You gave me a nil!" if x.nil?
  puts "thanks for the #{x}"
end

i_dont_take_nils_from_nobody nil
 #=> ArgumentError: You gave me a nil!
i_dont_take_nils_from_nobody 1
 #=> thanks for the 1

Problem

I previously worked a lot with Java and now I am working more with Ruby. One thing I cannot figure out though is what is the ruby equivalent to the Java "NullPointerException"? I want to test variables when I enter a function and if they are nil I want to raise this type of exception. Is there a specific ruby error class to raise this type of exception?

Original source