REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 18-Feb-2009 Edit History  

REBOL 3 Concepts: Tour: Objects

Pending Revision

This document was written for R2 and has yet to be revised for R3.

OLD DOCS

This section is obsolete and being replaced. It is kept only as a source for new content.

An object is a set of variables that have specific values in a context. Objects are used for managing data structures and more complex behavior. The following example shows how a bank account is set up as an object to specify its attributes and functions:

account: make object! [
  name: "James"
  balance: $100
  ss-number: #1234-XX-4321
  deposit:  func [amount] [balance: balance + amount]
  withdraw: func [amount] [balance: balance - amount]
]

In the above example, the words name, balance, ss-number, deposit, and withdraw are local variables of the account object. The deposit and withdraw variables are functions that are defined within the object. The variables of the account can be accessed with a path, as shown in the next example:

print account/balance
$100.00
account/deposit $300

print ["Balance for" account/name "is" account/balance]
Balance for James is $400.00

The next example shows how to make another account with a new balance but with all other values remaining the same:

checking-account: make account [
    balance: $2000
]

Notice that the new object is called checking-account, and it shares the values of the original account, but provides a new balance amount.

Just as easily you can create a new account object that extends the old account object, adding new variables for the bank name and last activity date:

account: make account [
    bank: "Savings Bank"
    last-active: 20-Jun-2000
]

print account/balance
$400.00
print account/bank
Savings Bank
print account/last-active
20-Jun-2000


  TOC < Back Next > REBOL.com - WIP Wiki Feedback Admin