REBOL 3 Docs | Guide | Concepts | Functions | Datatypes | Errors |
TOC < Back Next > | Updated: 11-Aug-2010 Edit History |
Evaluates a block, file, URL, function, word, or any other value.
Arguments:
value [any-type!] - Normally a file name, URL, or block
Refinements:
/args - If value is a script, this will set its system/script/args
arg - Args passed to a script (normally a string)
/next - Do next expression only, return it, update block variable
var [word!] - Variable updated with new block position
See also:
The do function evaluates a script file or a series of expressions and returns a result.
It performs the fundamental interpretive action of the REBOL language and is used internally within many other functions such as if, case, while, loop, repeat, foreach, and others.
Most of the time do is used to evaluate a script from a file! or url! as shown in these examples:
do %setup.r
Settings done.
do http://www.rebol.com/speed.r Console: 0:00:01.609 - 314 KC/S Processor: 0:00:00.406 - 2128 RHz (REBOL-Hertz) Memory: 0:00:00.657 - 72 MB/S Disk/File: 0:00:00.234 - 130 MB/S
Note that do of a file! or url! requires that the script contain a valid REBOL header; otherwise, you'll get an "Script is missing a REBOL header" error.
The do function can also be called to evaluate other types of arguments such as a block!, path!, string!, or function!.
do [1 + 2]
3
do "1 + 2" ; see special note below
3
Expressions are evaluated left to right and the final result is returned. For example:
do [1 + 2 3 * 4]
12
To obtain all results, use the reduce function instead.
print reduce [1 + 2 3 * 4]
3 12
Selecting a block to evaluate:
blk: [
[print "test"]
[loop 3 [print "loop"]]
]
do first blk
test
do second blk loop loop loop
The /args refinement allows you to pass arguments to another script and is used with a file, or URL. Arguments passed with /args are stored in system/script/args within the context of the loaded script.
The /next refinement returns a block consisting of two elements. The first element is the evaluated return of the first expression encountered. The second element is the original block with the current index placed after the last evaluated expression.
Evaluating strings is much slower than evaluating blocks and values. That's because REBOL is a symbolic language, not a string language. It is considered bad practice to convert values to strings and join them together to pass to do for evaluation. This can be done directly without strings.
For example, writing code like this is a poor practice:
str: "1234 + "
code: join str "10"
do code
1244
Instead, just use:
blk: [1234 +]
code: join blk 10
do code
1244
In other words, you can join values in blocks just as easily as strings.
TOC < Back Next > | REBOL.com - WIP Wiki | Feedback Admin |