REBOL 3 Docs | Guide | Concepts | Functions | Datatypes | Errors |
TOC < Back Next > | Updated: 3-Aug-2010 Edit History |
Returns the word or block in the object's context.
Arguments:
object [any-object! block!]
word [any-word! block! paren!]
See also:
Return the word from within another context. This function is normally used with set and get.
set-console: func ['word value] [ set in system/console word value ] set-console prompt "==>" set-console result "-->"
This is a useful function for accessing the words and values of an object. The in function will obtain a word from an object's context. For example, if you create an object:
example: make object! [ name: "fred" age: 24 ]
You can access the object's name and age fields with:
print example/name
print example/age
24
But you can also access them with:
print get in example 'name
print get in example 'age
24
The in function returns the name and age words as they are within the example object. If you type:
print in example 'name
name
The result will be the word name, but with a value as it exists in the example object. The get function then fetches their values. This is the best way to obtain a value from an object, regardless of its datatype (such as in the case of a function).
A set can also be used:
print set in example 'name "Bob"
Bob
Using in, here is a way to print the values of all the fields of an object:
foreach word words-of example [
probe get in example word
]
24
Here is another example that sets all the values of an object to none:
foreach word words-of example [ set in example word none ]
The in function can also be used to quickly check for the existence of a word within an object:
if in example 'name [print example/name]
none
if in example 'address [print example/address]
This is useful for objects that have optional variables.
In R3, in can also be used for binding a block to an object to support this useful idiom:
do in example [age + 1]
25
Identically, a paren! can be used as the rebound block:
do in example second [(age + 1) (age + 20)]
44
TOC < Back Next > | REBOL.com - WIP Wiki | Feedback Admin |