REBOL Programming/The Console

From Wikibooks, open books for an open world
< REBOL Programming
Jump to: navigation, search

The Console[edit]

Rebol console

The interactive console is where you can do much of the programming needed to test small segments of code and to try out various functions. It's the best way to learn REBOL functions.

If you have REBOL/View you can access various utilities from the console, such as the Desktop, a text editor and basic utilities for inputs, usable in your programs.

How to access the Console[edit]

The console can be accessed directly when you start REBOL/Core using Rebol.exe but this will not allow you to use the desktop or editor {} commands.

If you start REBOL/View, it will start from the Desktop (also known as Viewtop), but the console can be accessed in the left side of the window with the bottom icon. You can return to Viewtop by entering the desktop command at the console prompt.

This is the first window that you'll see starting Rebol/View:


Then click on the console icon.

Using the Console[edit]

Some important basics about the console are nice to know before starting with REBOL programming. You'll be spending a lot of time with the console!

Simply by typing commands, you can start using the console. If you type:

>> now

REBOL will return the current date and time

== 2-Sep-2005/2:12:42+2:00

A different example:

>> print "Hello world!"
Hello world!

Math is possible:

>> 2 + 2
== 4

Notice the >> and == symbols:

This is your prompt where you enter functions and commands.
This is a symbol for a returned value frorm the previously executed program or operation. You can use returned values to do further processing. More on this later in this book.

The print function we used earlier didn't produce a symbol before the text, because it's program output directed at the console. It's not a return value. The return value of the print function is a special value ignored by the interpreter when displaying results.

Let's try something that'll cause an error:

>> 2 / 0
** Math Error: Attempt to divide by zero
** Near: 2 / 0

REBOL kindly produces an error information text using the ** symbol.

History commands[edit]

If you want to retry these examples again, use the cursor up key on your keyboard to access REBOL's history of executed functions.

To move the console scroller you can use you mouse or the Page-Up and Page-Down keys

To display previously entered lines you can use the Up and Down arrows, like on a Linux console.

To modify them use the Home and End keys with Delete, Backspace and left or right arrows.

TAB auto-complete[edit]

The TAB key will start the autocompletation: rebol will try to complete what you are typing. For example if you press just a and then you press TAB, you'll get a suggestion of all words starting with a:

>> action! any-type! any-word! any-function! any-string! any-block! action? any-type? any-word?
any-function? any-string? any-block? and add and~ absolute at alias all any as-string as-binary
access-os arccosine arcsine arctangent abs as-pair about apply also append ajoin array alter 
attempt ask ascii? any-path!  any-path? any-object! any-object? assert alert aqua alive?

So, if you digit ale, it will complete with alert, the only word starting with ale:

>> alert

If you declare a variable, like aaa, the console will memorize it and it will auto-complete also with that. This is useful to see if your already used some variable name.

Console Help Functions[edit]

The console can help you with many things using the word 'help' or its shortcut the question mark ?:

1- The presence of a word in the REBOL dictionary and its contents:

>> ? searched-for-word
Print result here
>> ? day
No information on day (word has no value)
>> ? pi
PI is a decimal of value: 3.14159265358979
>> my-word: "beginning"
== "beginning"
>> ? my-word
MY-WORD is a string of value: "beginning"

2- The syntax of a function:

>> ? now
   NOW /year /month /day /time /zone /date /weekday /yearday /precise

    Returns the current local date and time.
    NOW is a native value.

    /year -- Returns the year only.
    /month -- Returns the month only.
    /day -- Returns the day of the month only.
    /time -- Returns the time only.
    /zone -- Returns the time zone offset from GMT only.
    /date -- Returns date only.
    /weekday -- Returns day of the week as integer (Monday is day 1).
    /yearday -- Returns day of the year (Julian)
    /precise -- Use nanosecond precision

3- If you type part of an element in a string, REBOL will return any element with that string:

>> ?? part-of-searched-word
Print result here
>> ? wor
Found these words:
  any-word!       datatype! any-word!
  any-word?       action!   Returns TRUE for any-word values.
  get-word!       datatype! get-word!
  get-word?       action!   Returns TRUE for get-word values.
  lit-word!       datatype! lit-word!
  lit-word?       action!   Returns TRUE for lit-word values.
  my-word         string!   "beginning"
  set-word!       datatype! set-word!
  set-word?       action!   Returns TRUE for set-word values.
  to-get-word     function! [value]
  to-lit-word     function! [value]
  to-set-word     function! [value]
  to-word         function! [value]
  word!           datatype! word!
  word?           action!   Returns TRUE for word values.

You can also use quotes to search for strings. Notice the difference between these two examples:

>> ? "find"
Found these words:
  find            action!   Finds a value in a series and returns the series a...
  find-key-face   function! Search faces to determine if keycode applies.
  find-window     function! Find a face's window face.
>> ? find
   FIND series value /part range /only /case /any /with wild /skip size /match /tail /last /reverse
    Finds a value in a series and returns the series at the start of it.
    FIND is an action value.
    series -- (Type: series port bitset)
    value -- (Type: any-type)
    /part -- Limits the search to a given length or position.
        range -- (Type: number series port)
    /only -- Treats a series value as a single value.
    /case -- Characters are case-sensitive.
    /any -- Enables the * and ? wildcards.
    /with -- Allows custom wildcards.
        wild -- Specifies alternates for * and ? (Type: string)
    /skip -- Treat the series as records of fixed size
        size -- (Type: integer)
    /match -- Performs comparison and returns the tail of the match.
    /tail -- Returns the end of the string.
    /last -- Backwards from end of string.
    /reverse -- Backwards from the current position.

Other useful information about the current work session can be found using the following REBOL words:

probe       : Displays the contents of a REBOL word in its original format.
source      : Displays the contents of a REBOL function.
what        : Lists all REBOL words currently defined.
what-dir    : Displays name of the current directory.
list-dir    : Lists all filenames in current directory.
change-dir  : Change current directory.

Stopping Programs[edit]

You can stop a program, if it's run from the console, or if it outputs to a console opened from within the program, by pressing Escape. This will return you to the prompt.

Console Behaviour[edit]

The console can fetch information from a configuration file to change its behaviour. This should probably be moved to a different chapter.