The String class also inherits behavior from its ancestor
classes. Indeed String is a subclass of
CharacterSequence, itself a subclass of
SequenceCollection . The direct consequence is that when
searching for some specific behavior, you may need to explore the
parent classes too. The whole behavior of a class, defined in the
class itself and its parents is called its protocol.
Again the browser is helpful to explore a class protocol. You have two options:
String class →
right mouse button → Browse protocol
(p)... Alternatively, use the keyboard shortcut Ctrl-p.
Figure 4.1: Browse String protocol
The new window is a protocol browser for the String
class. At the left, we see a hierarchy of the String’s ancestor
classes.
At the right are the method selectors for strings and, in parenthesis,
the class where they are defined. Methods defined in class
String itself are in bold characters.
Selecting one class there only shows the protocol starting from
this class down to the String class. If you select
String in the left panel, you only see methods defined in
the String class itself.
In Figure 4.1, no specific class is selected,
therefore the whole String protocol is listed at the
right. The method before: implemented in
SequenceableCollection is selected and its source code is
displayed on the large bottom pane.
String class →
right mouse button → Browse hierarchy
(h)... Alternatively, use the keyboard shortcut Ctrl-h or
the button hierarchy on the system browser.
Figure 4.2: Browse the String hierarchy
The hierarchy browser is very like the system browser with only two differences:
String is
printed. It makes it easy to browse String parent and
child classes.
The hierarchy browser is a general tool for exploration. Unlike
the protocol browser, it does not display the entire protocol
of a class. No inherited methods are shown, only those
defined directly in the selected class. In Figure 4.2, the class
SequenceableCollection is selected as well as its method
before:.
The before: method extracts from a collection the element before a specified
element. When inherited in String, those elements are
Character instances:
'1 + 3i' before: $i ⇒ $3
Practice the tools and resolve the exercise below.
Find the appropriate method to transform ’Hello My Friend’ into ’My Friend’.
Exercise 4.1: Cut a string
Beware, some messages in the String protocol may obviously not
work. Observe below, that the error is thrown on a Character
instance:
'Hello My Friend' cos ⇒ MesageNotUnderstood: Character>>cos
If you look at implementors of cos, you can
find that Collection expects to apply cos
to each member of a collection, hence a character is
asked for its cosine.
Symbol. A symbol is very like a string but it is unique and
never duplicated.
Two references to 'hello' might be to two or only
one object depending on computational history. Two references
to #hello are guaranteed to always refer to the same object.
Symbols got their name because they are used as symbolic constants. You already observed how in the book we wrote message selectors as a symbol. We use symbols because each message name must uniquely index the code for a method. You will use a symbol when you need to name something uniquely.
In the example below, observe the same behavior with string:
'hello' == 'hello' copy ⇒ false #hello == #hello copy ⇒ true
Now you know. A symbol can’t be duplicated nor changed. Symbols can contain space characters:
'hello my friend' asSymbol ⇒ #'hello my friend'
Symbol is a subclass of String and much of its behavior
is inherited. As we learn about Strings we
are also learning quite a bit about symbols.
Note that many String methods are defined to return strings.
'hello my friend' class. ⇒ String #'hello my friend' class. ⇒ Symbol #'hello my friend' asCamelCase ⇒ 'helloMyFriend' #'hello my friend' asCamelCase asSymbol ⇒ #helloMyFriend