Where are system globals (Smalltalk and SystemOrganisation)

Richard A. O'Keefe ok at cs.otago.ac.nz
Fri Dec 10 01:08:36 UTC 2004


Concerning this code:

	| globalObjects keyPrintString |
	
	globalObjects := Smalltalk reject: [:item | item class isMeta ].
	globalObjects  associationsDo: 
		[:assoc  | 
		 assoc halt.
		 keyPrintString := assoc key printString.
		 Transcript show: keyPrintString.
		 Transcript cr]
	
If we want to explain this to a beginning, we should start with something
rather simpler.  My advice would be that #associationsDo: is almost _always_
a bad thing to use.  Why go out of your way to ask for associations when you
don't actually _want_ associations?

A dictionary is a mapping from "keys" to "values".
Iteration over a dictionary using #do: gives you the values one at a time.
Iteration over a dictionary using #keysDo: gives you the keys one at a time.
Iteration over a dictionary using #keysAndValuesDo: gives you corresponding
keys and values, a matching pair at a time.

All we want in this case is the keys.  So

    globalVariables := Smalltalk reject: [:each | each class isMeta].

    globalVariables keysDo: [:key |
	"We want to print a key"
    ]

But what are these keys?  Well, since Smalltalk is a mapping from
global variable names to their values, the keys must be variable names.
What kind of object is a variable name?

Well,

    globalVariables keys "set of keys" anyOne "any member" class "what is it"
gives the answer
    Symbol

What's a Symbol?  It's a unique read only string.
The ANSI Smalltalk standard allows
    selectors like #foo #+ #at:put:
and
    symbols like #'Happy Birthday'
to be different, but historic Smalltalks made them the same, and so does
Squeak.  In any case, you don't need to convert a Symbol to a String because
it already _is_ a kind of string.

So we can do

    globalVariables keysDo: [:key | Transcript show: key; cr].

But there's an even easier thing to do.  Writing things to a Transcript
isn't a very object-oriented thing to do, because all you get is a bunch
of text you can look at.  There isn't much else you can do with it.

Nope, the best thing to do is

    globalVariables keys asSortedArray<inspect it>
or
    globalVariables keys asSortedArray<explore it>

To <inspect it>, the simplest thing is to type cmd-i,
or you can use the menu.
To <explore it>, the simplest thing is to type cmd-I (capital I),
or you can use the menu.

In fact, the _best_ thing to do is probably just

    globalVariables<explore it>

which will show you the variable names _and_ their values.
and the whole thing then simplifies to

    Smalltalk reject: [:each | each class isMeta]<explore it>

<explore it> is an extremely useful tool, and the sooner a beginner learns
to use it the better.  When you select something in the explorer, you can
type expressions in the bottom pane, and in those expressions, 'self'
refers to the thing you selected in the upper pane.  So you can not only
_see_ things (as you could if you printed them in a Transcript), you can
send them messages.
	



More information about the Squeak-dev mailing list