Vibescript Showcase
Resilient lookups
Recover from raised errors with rescue modifiers and probe float edge cases with nan? and finite?.
Source
showcase/reliability/resilient_parse.vibe
# title: Resilient lookups
# category: Vibescript Showcase
# difficulty: Showcase
# summary: Recover from raised errors with rescue modifiers and probe float edge cases with nan? and finite?.
# description: 1.0 makes fetch raise on a miss and float division follow IEEE 754, so the rescue modifier and the float predicates are the idiomatic way to keep tolerant code readable.
# tags: reliability, errors, floats
# vibe: 0.4
def lookup_or(table: hash, key: symbol, fallback)
table.fetch(key) rescue fallback
end
def safe_divide(a: int, b: int)
(a / b) rescue nil
end
def run
prices = { small: 5, large: 9 }
infinity = 1.0 / 0.0
not_a_number = 0.0 / 0.0
{
known: lookup_or(prices, :small, 0),
unknown: lookup_or(prices, :huge, 0),
divide_ok: safe_divide(100, 4),
divide_guarded: safe_divide(100, 0),
infinity_label: infinity.to_s,
is_finite: (3.0 / 4.0).finite?,
nan_detected: not_a_number.nan?,
incomparable_is_nil: (1 <=> "a") == nil
}
end
Output
Press run to execute run from this example.