Today's Smalltalk 4 You continues looking at the basic Smalltalk class libraries in VA Smalltalk - with the focus on write streams If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:
You can also watch it on YouTube:
Today we'll look at the Stream class hierarchy, focusing on write streams. To get started, we want to browse the Stream hierarchy, in order to get a full picture of the local and inherited APIs:/p>
We are going to focus on the API methods (as per the ANSI specification, and experiment with a few simple examples in a workspace. The workspace code we'll use follows:
"writing" wStream := WriteStream on: String new. wStream nextPut: $3. wStream nextPutAll: ' foo bar baz'. wStream cr. wStream contents.
Here we are writing text (Strings and Characters) to a stream, but you can put anything on an internal stream (for external ones, make them binary to do the same thing). Here's what running the above will give you if you inspect the results:
The most important writing methods to understand are:
- #nextPut: - write one element to the stream
- #nextPutAll: - sent with a collection as an argument, write it to the stream
- contents - get all elements on the stream
It's worth trying a few experiments of your own - and notice that streams are not just about text. You can stream over any collection in Smalltalk - try creating a write stream and putting numbers on it, and see what you get back with #contents
Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.
[st4u180-iPhone.m4v ( Size: 4315408 )]