Functions
Introduction
This guide explains how to create and use functions in rulesets.
Functions are a set of statements that perform a task or calculate a value and are useful to combine some functionality that you use multiple times in your ruleset. Functions are comparable to JavaScript functions, and you can create them yourself in Rulecube and use them anywhere you need their functionality.
Just one bit of info before you start creating a function. Functions need input to operate on, and an input for a function is called a parameter. So when calling a function, you supply its parameters, and the function returns a calculated or determined result.
Procedure
Let's say you're developing a ruleset that determines lending risks, and you need to calculate the so-called debt-to-income (DTI) ratio at several places. The DTI ratio is a person's monthly debt payments divided by the gross monthly income. You can add this calculation anywhere you need, but it's much more efficient if you create a function for this and call it when needed.
Create a ruleset named
RiskAssessment
.Create a function named
DTI
, and in the Return type list, selectnumber
.Click Add parameter. ↳ Rulecube adds a parameter row:
In the Name field, enter
monthlyDebtPayments
, and in the Type list, selectnumber
.Add a second parameter named
grossMonthlyIncome
, and also set its Type tonumber
.In the Function body, enter the following function:
monthlyDebtPayments / grossMonthlyIncome * 100
Explanation In general, the DTI is calculated as a percentage as in this calculation.
Click Save. ↳ The DTI function is now ready to be called by its name, providing the required parameters. For example, from a rule Expression with
Payments
andIncome
as ruleset inputs which you supply as parameters:DTI(Payments, Income)
Additional resources
Rulecube has a list of built-in functions called methods: How-to Guide: Use Methods.
You can combine your functions into one ruleset and designate this ruleset as a library. This way, you enable your functions to be called from any ruleset: Reference Guide: Libraries.