Sunday 15 May 2011

pharo - SmallTalk - Printing contents of an OrderedCollection object using Transcript -



pharo - SmallTalk - Printing contents of an OrderedCollection object using Transcript -

|oc| oc := orderedcollection new. oc add: 2. oc add: #(4 9). oc transcript show: self; cr.

upon running next code in pharo, getting message:

messagenotunderstood: orderedcollection>> transcript

when replacing 'self' 'oc' still getting same error. looking way output collection using transcript.

why can't transcript receiver of code?

remember object message: parameter syntax: you're trying send transcript message oc object, , send show: message object returned that, self parameter.

what want inquire transcript object show: oc object. so, that: send transcript show: message oc parameter: transcript show: oc. show string representation of collection.

if print each fellow member of collection (instead of printing the collection itself), should utilize do: method iterate on them: oc do: [ :element | transcript show: element ]. here print each of collection's member string representation.

oc := orderedcollection new. oc add: 2. oc add: #(4 9). transcript show: 'show collection:'; cr. transcript show: oc; cr. transcript show: 'show each element:'; cr. oc do: [ :element | transcript show: element; cr ].

gives output:

show collection: orderedcollection(2 #(4 9)) show each element: 2 #(4 9)

smalltalk pharo

No comments:

Post a Comment