Rebol Programming/The console

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

The console[edit | edit source]

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 | edit source]

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 Viewtop. The console can be accessed in the left side of the window with the bottom icon. To get to the console, click on the icon. You can return to Viewtop by entering the desktop command at the console prompt.

Using the console[edit | edit source]

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 from the previously executed program or operation.

The print function we used earlier just produced output at the console. 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 | edit source]

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 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 | edit source]

The TAB key can be used for autocompletion: 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 type ale, it will complete with alert, the only word starting with ale:

>> alert

If you declare a variable, like aaa, Rebol will memorize it and it will auto-complete also with that.

Console help functions[edit | edit source]

The console help can be accessed using the help command or its shortcut, the question mark ?. It can:

1- Find a word in the Rebol dictionary and describe its contents. Examples:

>> ? 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- Describe 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 | edit source]

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 the Escape key. This will return you to the prompt.