s LearnSqueak.com

ABSOLUTE BEGINNER

Your first text replacement

Change a word inside some text with one message. Try five tiny examples and keep the original when you need it.

5 minutes · 5 small steps · Squeak 6.1

STEP 1

Start with a greeting

Open Workspace. Type this text, select it, and choose Print it.

Select the whole expression, then Print it
'Hello, cat!'
NATIVE SQUEAK CAPTURE
Native Squeak text replacement checkpoint 1
Native Squeak Workspace showing the code and its actual evaluated result.
You should see

The greeting appears as text.

STEP 2

Change one word

Send copyReplaceAll:with: to make a changed copy.

Select the whole expression, then Print it
'Hello, cat!' copyReplaceAll: 'cat' with: 'dog'
NATIVE SQUEAK CAPTURE
Native Squeak text replacement checkpoint 2
Native Squeak Workspace showing the code and its actual evaluated result.
You should see

The greeting now says dog.

STEP 3

Change every match

The message replaces each matching piece of text.

Select the whole expression, then Print it
'red red blue' copyReplaceAll: 'red' with: 'green'
NATIVE SQUEAK CAPTURE
Native Squeak text replacement checkpoint 3
Native Squeak Workspace showing the code and its actual evaluated result.
You should see

Both copies of red became green.

STEP 4

Notice capital letters

Capital letters and small letters are different here.

Select the whole expression, then Print it
'Cat cat' copyReplaceAll: 'cat' with: 'dog'
NATIVE SQUEAK CAPTURE
Native Squeak text replacement checkpoint 4
Native Squeak Workspace showing the code and its actual evaluated result.
You should see

Cat stays the same. Only cat changes.

STEP 5

Keep both versions

Give the text a name, then look at the original and the changed copy.

Select the whole expression, then Print it
| greeting |
greeting := 'Hello, cat!'.
{greeting. greeting copyReplaceAll: 'cat' with: 'dog'}
NATIVE SQUEAK CAPTURE
Native Squeak text replacement checkpoint 5
Native Squeak Workspace showing the code and its actual evaluated result.
You should see

The original still says cat. The second text says dog.

YOU DID IT

A small step. A real result.

copyReplaceAll:with: makes a new piece of text. The original stays unchanged.

Optional code download: open it as text and copy one example into Workspace.

Download the five examples; copy one at a time

Your first list · More Smalltalk messages