Examining objects
Sometimes, you must inspect the state of an object to determine the cause of an error. There are two ways to inspect the contents of objects while testing your applications:
• By adding trace messages to your scripts
• By using the object inspector
Trace messages
The simplest way to view an object's state is by using Transcript show: messages to display the object's instance variables.
For practice, Execute this flawed example in the System Transcript:
| aCollection |
aCollection := OrderedCollection new
add: 'Dog';
add: 'Cat'.
Transcript show: aCollection printString; cr.
Transcript show: aCollection size printString; cr.
In each of the last two statements, the message printString is used to convert an object that is not a string into a printable string. The string is then passed as an argument to the show: message. The cascaded message cr causes a carriage return in the System Transcript.
Using the inspector
To open an Inspector window, send the message inspect to any object. For example, Execute the following statement in the System Transcript window:
#('Dog' 'Cat' 'Zebra') inspect.
The following window appears:
As you click on each array index (or variable) in the left-hand list, its value appears in the right-hand text area. You can also double-click on one of the variables to expand its contents. Double-clicking on a variable that does not have ... after it brings up an inspector on just that variable.
In some cases, you can even type new values in the right-hand text area and select Save from the pop-up menu to change the value of the object.
You can inspect any object in any script just by adding a statement to the script that sends the inspect message to the object.
Inspector windows let you watch the state of an object while testing your application. If you leave the inspector window open during testing, you can monitor the object's state as your application runs.
For more information on Inspector windows, refer to the Smalltalk User Guide.
Last modified date: 07/23/2020