Problems with single quotation marks, categories, and methods

Les Tyrrell tyrrell at canis.uiuc.edu
Thu Feb 12 17:03:27 UTC 1998


Try:  'I''m' and inspect the result, rather than print it.

You are being fooled by the printed form of I'm, rather than
it's actual content.  The giveaway is the single qoutes
surrounding your result- this is the printString version.
The doubled single qoutes are working as they should to
produce the single single quote.

You will run into this anytime you have a string, and you
are trying to print it.  It will be bracketed by the single
quotes.  For instance, suppose you were streaming along:


	| aStream |
	
	aStream := WriteStream on: ( String new: 128 ).

	aStream print: 'I''m not quite right!'.
	
	aStream contents.
	
The result, if print'd from the pop-up menu would be:

	'''I''''m not quite right!'''

but the inspect'd result would be, with quotes and all:

	'I''m not quite right!'

This probably is not what you want.  Instead, do this:

	| aStream |
	
	aStream := WriteStream on: ( String new: 128 ).

	aStream nextPutAll: 'I''m ok!'.
	
	aStream contents.

This time, what you get for the print'd result is

	'I''m ok!'

And the inspect'd result is:

	I'm ok!

Why the difference?  The #print: message for String gives
a result that can be compiled to recreate the string.
So whatever you wrote to create the string, is the result
you get back whenever you print'd it from the workspace menu.
When you inspect'd the string, you actually saw what it
really was.

#nextPutAll: simply iterates over the string, putting each
element of the string onto the stream, just as it is.
It does not create a form of the string that can be
used to recreate the string.

So, in actuality the first two examples you gave are doing
the right thing, but the last won't work for a different
reason: the #, message expects a collection as an argument,
and $' is (obviously) not a collection.  Good try, though!
(I've written that lots of times...).  While it is a tempting
form, in actuality it would lead to a bad programming practice
if it were acceptable to do this.  The problem is in the
way space is allocated for collections when growing-
Streams will double the size, while the concatenation only
asks for enough to cover the immediate need.  If you are
doing lots of concatenating you will see tremendous differences
in performance due to the huge number of objects created and
then immediately discarded when using #, instead of a stream.

Hope this helps!

les





More information about the Squeak-dev mailing list