Module: Bang::ProcMixin

Included in:
Proc
Defined in:
lib/bang.rb

Overview

Mixin for Proc class that adds ‘#raises?`, `#rescues?` and `#throws?`.

Instance Method Summary collapse

Instance Method Details

#raises?(exception) ⇒ true, false

Execute this procedure and return ‘true` if the specific given `exception` is raised, otherwise `false`.

Returns:

  • (true, false)

    Whether exception was raised.



258
259
260
261
262
263
264
265
266
267
# File 'lib/bang.rb', line 258

def raises?(exception)
  begin
    call
    false
  rescue exception => err
    exception == err.class
  rescue Exception => err
    false
  end
end

#rescues?(exception) ⇒ true, false

Execute this procedure and return ‘true` if the given `exception`, or subclass there-of is raised, otherwise `false`.

Returns:

  • (true, false)

    Whether exception was rescued.



275
276
277
278
279
280
281
282
283
284
# File 'lib/bang.rb', line 275

def rescues?(exception)
  begin
    call
    false
  rescue exception => err
    true
  rescue Exception => err
    false
  end
end

#throws?(object) ⇒ true, false

Execute this procedure and return ‘true` if the given `object`, is thrown, otherwise `false`.

Returns:

  • (true, false)

    Whether object was thrown.



292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/bang.rb', line 292

def throws?(object)
  pass = true
  catch(object) do
    begin
      call
    rescue ArgumentError => err     # 1.9 exception
      #msg += ", not #{err.message.split(/ /).last}"
    rescue NameError => err         # 1.8 exception
      #msg += ", not #{err.name.inspect}"
    end
    pass = false
  end
  pass
end