Rebol Programming/Language Features/Objects

From Wikibooks, open books for an open world
< Rebol Programming‎ | Language Features
Jump to navigation Jump to search


Objects are a very common way to manage and access data in Rebol. In fact most of the Rebol system is built with objects. The SYSTEM object can be considered the spine of Rebol, the place where all non-core functions and data reside. It's the place you can find all of VID, the various networking protocols and all mezzanine functions.

Using Objects[edit]

Objects are made with the MAKE function.


>> shopping-basket: make object! [fruits: 5 vegetables: 3]

Note that nothing is returned on the console. Objects can hold all types of data, even other objects.

A bigger example, as you'd normally write it in your program code:

shopping-basket: make object! [
  fruits: 5
  vegetables: 3
  dairy-products: 7

Accessing Objects[edit]

If you want to view an object, you might have noticed that typing the name in doesn't return anything:

>> shopping-basket

You can use different methods for viewing the object data:

>> probe shopping-basket
make object! [
  fruits: 5
  vegetables: 3

A nicer overview can be returned with:

>> ? shopping-basket
SHOPPING-BASKET is an object of value: 
   fruits          integer!  5 
   vegetables      integer!  3 
   total           integer!  8 


>> print dump-obj shopping-basket 
   fruits          integer!  5 
   vegetables      integer!  3 
   total           integer!  8

These functions can't be used in programs to get values from an object, since they only print information in the console. They don't produce a return value.

One way is to use path notation:

>> shopping-basket/fruits
== 5

There are multiple ways to access object data, both for manipulation and viewing.

Referring to Self[edit]

An object can refer to itself or other variables and functions within itself using SELF:

>> shopping-basket: make object! [fruits: 5 vegetables: 3 total: self/fruits + self/vegetables]
>> shopping-basket/total
== 8

Note, that the usage of SELF in the above example is not necessary, though.