Getting started -- a fact store
Interactive program that stores facts
Extending the initial example -- a clipboard text tool
A persistent clipboard text tool -- a further extension
The following program defines the class store.
The class remembers facts associated with keys.
Entries may be recalled, forgotten, or listed.
-- store.roo store : class extends Map initialize : method say 'The store is ready.' return '' remember : method select when arg() = 2 then do -- ADD an association v = ^ add( arg(1), arg(2) ) ^ save return v end when arg() = 1 then -- RECALL association return ^ getAt( arg(1) ) otherwise call raiseObjection 'STORE: 1 or 2 arguments are expected.' end forget : method v = ^ removeAt( arg(1) ) -- FORGET association ^ save return v listKeys : method keys = '' loop ix over ^ self keys = keys ix end return keys listAll : method return ^ toDelimitedString( '0d0a'x ) -- LIST associations save : method -- default 'save' method does nothing return ''
The following program is an interactive main program that uses the store class.
-- store1.rooProgram store = ^^ store -- prepare the 'store' object call showUsage signal on halt do forever parse linein request +1 key '=' value key = strip( key ) value = strip( value ) select when request = '' then nop when request = '+' then store ~ remember( key, value ) when request = '?' then say store ~ remember( key ) when request = '-' then store ~ forget( key ) when request = 'k' then say store ~ listKeys when request = '!' then say store ~ listAll when translate( request ) = 'Q' then exit 0 otherwise call showUsage end end halt : exit 0 showUsage : procedure say 'Usage:' say ' + key = value -- adds a value' say ' ? key -- locates value associated with key' say ' - key -- forgets value associated with key' say ' k -- lists all keys' say ' ! -- lists all associations' say ' Q -- terminates' return
Here is an example session of the store program.
Keyboard requests are shown in bold text. Program responses are in plain text.
The store is ready.
Usage:
+ key = value -- adds a value
? key -- locates value associated with key
- key -- forgets value associated with key
k -- lists all keys
! -- lists all associations
Q -- terminates
+ address=box 1
+ city=eliot
+ state=me
+ zip=03903
? city
eliot
k
address city state zip
!
address=box 1
city=eliot
state=me
zip=03903
q