> For the complete documentation index, see [llms.txt](https://docs.rulecube.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rulecube.com/v2.5/tutorial/create-and-run-your-first-ruleset.md).

# Create and Run Your First Ruleset

## Introduction

Now you're going to create and run a ruleset, which will be based on the example from the previous tutorial—the decision of whether a loan applicant is eligible for a discount based on age. After creating the ruleset, you're going to run it by trying it out from within Rulecube.

By the end of this tutorial, you'll have a ruleset that determines if a loan applicant is eligible for a discount and returns the result as a number value. Zero if the applicant isn't eligible and 1.5 if they are.

Furthermore, you'll know how to:

* Create a ruleset.
* Set the input.
* Define a rule.
* Set the output.
* Try out a ruleset.

## Before you begin

* You must be logged in to Rulecube to create a ruleset

{% hint style="info" %}
Step-by-step, you're going to create a ruleset and then run it in the Sandbox to see what it does.
{% endhint %}

## Create a ruleset

Start off with creating a new ruleset:

1. Click **+ Ruleset** and select **Blank ruleset** to create a ruleset.\
   ↳ The **Add new ruleset** pop-up opens.
2. In the **Ruleset name** field, enter `Loan discount`.
3. Click **OK**.

Rulecube creates the ruleset and shows the *General settings* page for the ruleset.

Notice the **Input**, **Rules**, and **Output** sections in the menu on the left, which correspond to the core components discussed in the previous tutorial.

{% hint style="info" %}
When you haven't created any ruleset yet, Rulecube shows a page where you can click **Start** to create your first ruleset.
{% endhint %}

### Task: Set the input

Your ruleset is now nothing more than a skeleton, so add an input to work with:

1. Click the **+** sign behind **Input** from the left menu.\
   ↳ The **Input details** page opens:<br>

   <figure><img src="/files/Ii5KSgzcyZrd34OKG9Io" alt=""><figcaption></figcaption></figure>
2. In the **Name** field, enter `Age` and press `Enter`.\
   ↳ Rulecube changes the input's name to `Age` and shows this alert:<br>

   <figure><img src="/files/zVxvjg8PORJ6WLy6gVRd" alt=""><figcaption></figcaption></figure>

   This makes sense because you only created the input and haven't yet used it in the ruleset. But as you can see, Rulecube supports you with such alerts to create optimized rulesets.\
   \
   Also note that Rulecube shows the number![](/files/4OcADcPcxC866fzQBOHC)behind **Input** in the left menu, which indicates the number of inputs your ruleset has.

### Task: Define a rule and set the output

Okay, now it's time to put your ruleset to work by adding a rule:

1. Click the **+** sign behind **Rules** from the left menu.\
   ↳ The **Rule details** page opens.
2. In the **Name** field, enter `DiscountPercentage`.
3. Leave the **Type** to **number** to set the output type.
4. In the **Expression** field, enter this expression:<br>

   ```javascript
   Age < 35 ? 1.5 : 0
   ```

   \
   This is a so-called ternary expression with these parts:

   1. The condition followed by a question mark. In this case: `Age < 35`.
   2. The result when the condition is true, followed by a colon. In this case: `1.5`.
   3. The result when the condition is false. In this case: `0`.

   Another, longer way to write this expression would be:<br>

   ```javascript
   if (Age < 35) {
     return 1.5;
   }
   else {
     return 0;
   }
   ```

   **IMPORTANT**: note that we use the `return` statement here. When writing rules that are more complex than single-line expressions, you need to add the keyword `return` to the value you want the rule to have.
5. Click **Save**.\
   ↳ The **Save** pop-up opens.
6. Select the **Do not ask again** checkbox and click **OK**.

## Try out the ruleset

Let's try out your ruleset and see if it works as expected with these steps:

1. Click **Try it out!**\
   ↳ The **Sandbox** page opens.
2. In the **Age** field, enter `34` and click **Execute**.\
   ↳ Your rule is run and shows the expected value of `1.5` for the `DiscountPercentage` output:<br>

   <figure><img src="/files/xgWhoeZxaN2lw3ZTRb4O" alt=""><figcaption></figcaption></figure>
3. Now test the ruleset by entering `35` for age.\
   ↳ `DiscountPercentage` should be `0`, as expected.
4. Try zero and a negative value for age.\
   ↳ `DiscountPercentage` returns `1.5`, which is technically correct as the provided ages are less than 35. But it would be better if your ruleset signals such ages as incorrect, and you'll deal with that later.\
   \
   For now, you can suffice by modifying the expression so that ages of 0 and less result in a `DiscountPercentage` of `0`, which is neater and safer:
   1. Figure out how the condition should be to catch 0 and negative ages and modify the expression accordingly.&#x20;
   2. Try zero and a negative value for age again to test your ruleset.
   3. Also, test correct values such as `34` and `35` to ensure that these are still processed correctly.

<details>

<summary>A solution to the modified condition to catch 0 and negative ages</summary>

```
Age>0 && Age<35 ? 1.5 : 0
```

</details>

## Recap

You've created a ruleset with its core components and tried and tested it successfully from Rulecube.&#x20;

## What's next?

Now let's call the ruleset via its API as you would in practice in the next tutorial.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.rulecube.com/v2.5/tutorial/create-and-run-your-first-ruleset.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
