Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Basic Ruby Assert Function

DZone's Guide to

Basic Ruby Assert Function

·
Free Resource
A very simple way to add assert capability to function.

def assert
  raise "Assertion failed !" unless yield if $DEBUG
end

Usage :

def aFunc(i)
  assert { i < 10 }
  # ...
end

$DEBUG = true
# Ok.
aFunc(5)
# Raise Assert Exception.
aFunc(15)

$DEBUG = false
# Ok.
aFunc(5)
aFunc(15)
Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}