I need to catch a NameError in a special case. But I don't want to catch all SubClasses of NameError. Is there a way to achieve this?
# This shall be catched
puts 'Will do something with this error'
# This shall not be catched
# Will raise a NoMethodError but I don't want this Error to be catched
String.myattribute = 'value'
puts 'Should never be called'
You can re-raise exception if its class is different than given:
begin # your code goes here rescue NameError => exception # note that `exception.kind_of?` will not work as expected here raise unless exception.class.eql?(NameError) # handle `NameError` exception here end