REBOL 3 Docs | Guide | Concepts | Functions | Datatypes | Errors |
TOC < Back Next > | Updated: 6-Feb-2009 Edit History |
Variables within objects are accessed with paths. The path consists of the object name followed by the name of the variable. For example, the following code accesses the variables in the example object:
example/var1 example/var2
Here are examples using the bank-account object:
print luke/last-name
Lakeswimmer
print fred/balance
$510.00
Using a path, the variables of an object can also be modified:
fred/balance: $1000.00
print fred/balance
$1000.00
You can use the in function to access object variables by fetching their words from within their object context:
print in fred 'balance
balance
The balance word returned has the object fred as its context. You can get the value it holds by using get:
print get in fred 'balance
$1000.00
The second argument to the in function is a literal word. This allows you to dynamically change words depending on what is needed:
words: [first-name last-name balance] foreach word words [print get in fred word] FredSmith $1000.00
Each word in the block is used to obtain its value in the object.
The in function can also be used to set object variables.
set in fred 'balance $20.00
print fred/balance
$20.00
If a word is not defined within an object, the in function returns none! . This is useful for detecting when a variable exists within an object.
if get in fred 'bank [print fred/bank]
TOC < Back Next > | REBOL.com - WIP Wiki | Feedback Admin |