简体   繁体   中英

The right way to print a collection with delimiter in SmallTalk - Squeak?

I have been trying to redefine the printOn method by using this code:

coordinates do: [:elem | aStream print: elem] separatedBy: [aStream print: ' ,']

where "coordinates" stands for the name of the collection (OrderedCollection), but i was expecting to print this when calling to Transcript show:

(2/2) ,(1/1) ,(3/3) ,(-4/4)

I got this instead:

(2/2)','(1/1)','(3/3)','(-4/4)

i. e the quotes was printed as well.

I have tried to play with it, defining a local variable and use it, etc. Nothing worked.

Don't use #print: to print strings. Use #nextPutAll: to be safe (works for all collections). #print: writes a string representation on the stream and surrounds the argument with quotes, that's why you get quotes in the output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM