Friday, February 13, 2009

Class Announcement for Santa Fe area

This isn't a class offering from withaguide.com, but I think it would be of interest to anyone learning Smalltalk.

An Educational Offering From the
The Santa Fe Complex

Advanced Web Applications with GLASS*
March 30 to April 3

Building sophisticated multi-media, intensely interactive, Web applications is a significant challenge requiring mastery of multiple technologies. GLASS provides a single environment, and language, for developing desktop-like Web applications.

This workshop will provide a GLASS overview and tutorial followed by a combination of hands-on projects and in-depth coverage of special topics (e.g. the incorporation of Sun’s Lively Kernel technology into GLASS).

James Foster of Gemstone will lead the workshop. You will also have the opportunity to work with prominent members of the Santa Fe Complex on projects.

Dates and Times: Monday, March 30 – Thursday, April 2, 8:30-4:00. Friday, April 3, 8:30-noon. Informal discussions, explorations, and hacking likely to take place each evening.

Where: The Santa Fe Complex – 632 Agua Fria, in Santa Fe (one block from the RailRunner if you are coming up from Albuquerque)

Prerequisites: Some programming background will be helpful. Open to high school students.

Fee: $375 – includes lunch and breaks

Housing: Discounted rates at hotels (all within walking distance of the Complex) will be available.

For More Information:
E-mail Dr. Dave West at profwest@fastmail.fm or phone him at 505-231-7233.
Or come by the Santa Fe Complex, 632 Agua Fria in Santa Fe.

*Gemstone, Linux, Apache, Seaside and Smalltalk (http://seaside.gemstone.com)

Sunday, February 1, 2009

Characters, Strings, and Things

Sebastian Nozzi asked on the beginners list:
I've been struggling a bit with some basics about Strings for which I
couldn't find an answer (yet).

1) How to construct a String from Characters?

For example, I want to construct a String from the Chatacter literals:

$H $I
Character cr
$T $H $E $R $E

2) How to replace a sequence of Characters in a String for others?

For exaple, I want to replace every 'HI' (in this case only one) for
'HELLO' in the String above (not necesarily destructively, getting a
new String is also ok). Is there a quick way to do that?

Thanks in advance!
Smalltalk isn't known for being a scripting language. The runtime and object model are very similar to Ruby, but it is lacking all of the little Perl-isms that we associate with scripting languages.

To deal with the input characters, it is useful to have them in an array. A traditional Smalltalk array with characters would look like this #($a $b $c) but it isn't obvious what to do with the carriage return. For this, the Squeak brace array is handy and also works in Pharo.

characters := {$H.$I.Character cr. $T.$H.$E.$R.$E}.

It isn't terribly portable to other Smalltalks, but it sure is easy to type. Now we've got an array of characters, how to create the new String? Another Squeak-ism is the class method #streamContents:.
It takes a one argument block. The argument is a writeable stream that will return its contents at the end. I have to admit I was thrown by it the first time I saw it.

string := String streamContents: [:writeStream | characters do: [:c | writeStream nextPut: c]].
Like the brace array, it isn't very portable to other Smalltalks, but it is pretty handy.

That leaves us with replacing. I opened the method finder and typed replace into the search box.



That gives us the following complete solution:

| characters string |
characters := {
$H.$I.Character cr. $T.$H.$E.$R.$E}.
string := String streamContents: [:writeStream | characters do: [:c | writeStream nextPut: c]].
string copyReplaceAll: 'HI' with: 'HELLO'

Enjoy!