Method: RUIC#assert

Defined in:
lib/ruic.rb

#assert(condition = :CONDITIONNOTSUPPLIED, msg = nil, &block) ⇒ Object

Simple assertion mechanism to be used within scripts.

Examples:

1) simple call syntax

# Provides a generic failure message
assert a==b
#=> assertion failed (my.ruic line 17)

# Provides a custom failure message
assert a==b, "a should equal b"
#=> a should equal b : assertion failed (my.ruic line 17)

2) block with string syntax

# The code in the string to eval is also the failure message
assert{ "a==b" }
#=> a==b : assertion failed (my.ruic line 17)

Parameters:

  • condition (Boolean) (defaults to: :CONDITIONNOTSUPPLIED)

    the value to evaluate.

  • msg (String) (defaults to: nil)

    the nice error message to display.

Yield Returns:

  • (String)

    the code to evaluate as a condition.



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ruic.rb', line 135

def assert(condition=:CONDITIONNOTSUPPLIED,msg=nil,&block)
  if block && condition==:CONDITIONNOTSUPPLIED
    msg = yield
    condition = msg.is_a?(String) ? eval(msg,block.binding) : msg
  end
  condition || begin
    file, line, _ = caller.first.split(':')
    puts "#{"#{msg} : " unless msg.nil?}assertion failed (#{file} line #{line})"
    exit 1
  end
end