REBOL
Downloads Products Documents Community

REBOL Quick Start: Part 3 - Writing Scripts

By Carl Sassenrath
REBOL's Designer
Revised: 4-May-2010

The Plan

In the prior Quick Start: REBOL Scripts you learned how to make small changes to existing demo scripts.

In this article, I want to show you how to write REBOL scripts.

You Need a Text Editor

As you know, REBOL/View includes a script editor, but it's very basic. Yes, you could use the Windows Notepad editor, but to be the most productive, I suggest that you download a more powerful editor.

A selection of editors can be found here. Many of them support REBOL with with syntax colorizing to help you spot mistakes before you run your script, and direct evaluation of your scripts with a single key, like control-E.

Your First Script

Assuming you now have an decent editor, open it up and type:

REBOL []

alert "Hello World"

Save this script as a file and run it (press CTRL-E in your editor or click on the file's icon).

You should see an alert box open and display the text string.

Congratulations on your first script.

Spacing Rules

It turns out, you can write the above script as:

REBOL [] alert "Hello World"

or

REBOL []
alert
"Hello World"

REBOL does not care about lines or indentation. It is line break friendly.

However, REBOL does care about spacing. For example:

1 + 2 Is correct.

1+2 Is not correct. Will cause an error.

The general rule is: be sure to put spaces between all words and values. It's just like writing English (or other languages). That should make it easy to remember.

Why does REBOL need spaces like English? Because in REBOL you can create words such as total-amount. The dash does not indicate subtraction; it is just a hyphen. You can also write words like total+one or *new*.

In addition, REBOL allows you to express many special types of values. For example, you can write a date that includes a time and a timezone:

10-Aug-2006/10:32-4:00

That is a single value in REBOL. It is quite different from writing:

10-Aug-2006 / 10:32 - 4:00

That is a date divided by a time and subtracting a time.

So, as you can see, spaces are very important.

Your Second Script

Here's a new script:

REBOL []

birth: 10-Aug-1990

alert reform ["You are" now - birth "days old!"]

Save it and run it.

If you accidently typed now-birth, you will see:

** Script Error: now-birth has no value
** Where: reform
** Near: now-birth "days old!"

Don't forget that you need spaces between all values!

Brackets for Blocks

REBOL uses the square brackets [ ] for blocks. I picked the [ ] characters because they are lowercase and easy to type on US keyboards. (And, you will be typing them a lot.)

A block is simply a collection of words and values. Blocks are used for both code and data. You've seen blocks above used in the REBOL header and other parts of the code.

It is important not to forget that for every open bracket you need a matching closing bracket.

In the example above, if you forgot the closing bracket:

alert reform ["You are" now - birth "days old!"

When you run, REBOL will inform you:

** Syntax Error: Missing ] at end-of-script
** Near: (line 5) alert reform ["You are" now...

So, always double check your block brackets. (And some text editors will help you keep track of them. Check your editor manual.)

Blocks are part of a bigger concept in REBOL called series. You can find a lot more about that idea in REBOL Series.

Your Third Script

Let's make the next script a bit more interactive:

REBOL []

view layout [
    backcolor gold
    h2 "Web Bookmarks"
    style btn btn 130
    btn "REBOL.com" [browse http://www.rebol.com]
    btn "REBOL.net" [browse http://www.rebol.net]
    btn "REBOL.org" [browse http://www.rebol.org]
]

When you run it, you will see:


Click on the buttons to open your web browser to those web sites. Add your own new bookmarks to the list. Be sure to keep track of your block brackets.

Wait! What's That Strange Window?

Why does the above window image look oddly different? Because, it's showing REBOL running on Mac OS X.

When you write scripts in REBOL, they run identically over a wide range of systems including not only Windows, but Mac OS X, Linux, BSD, and more. There are obvious advantages to doing so.

This ability is called write once, run anywhere. (Some of you will recognize this term, WORA. More on it later.)

Spacing Conventions

Notice the indentation used in that last example. The lines inside the layout block are indented by four spaces. That is a REBOL convention; it is a standard style, not a strict rule.

The indentation helps show the contents of the block. The brackets themselves are not indented, because they are not part of the content. They are outside of the content.

The standard tab spacing for REBOL is four spaces (not eight spaces). Using four spaces is common in most languages, and any good editor will let you set that value as the default (when you press the tab key).

Next: Help from the Console

You now have a decent way to write REBOL scripts, and you have learned the main rules regarding spacing and the block format.

In the next tutorial, Part 4 - Help from the Console, I will show you how to interactively your code from the console and how to get help with it.

About | Contact | Support | Privacy | LicenseREBOL Technologies 2016