|oc|
oc := OrderedCollection new.
oc add: 2.
oc add: #(4 9).
oc Transcript show: self; cr.
Upon running the following code in Pharo, I am getting the message:
MessageNotUnderstood: OrderedCollection>> Transcript
When replacing 'self' with 'oc' I am still getting the same error. I am looking for a way to output the collection using the Transcript.
Why can't Transcript be the receiver of my code?
Remember the object message: parameter
syntax: you're trying to send the Transcript
message to the oc
object, and then send the show:
message to the object returned by that, with the self
parameter.
What you really want to do is ask the Transcript
object to show:
your oc
object. So, do that: send Transcript
the show:
message with oc
as a parameter: Transcript show: oc
. That will show the string representation of the collection.
If you were to print each member of the collection (instead of printing the collection itself), you should use the do:
method to iterate over them: oc do: [ :element | Transcript show: element ]
. Here you print each of the collection's member string representation.
oc := OrderedCollection new.
oc add: 2.
oc add: #(4 9).
Transcript show: 'Show the collection:'; cr.
Transcript show: oc; cr.
Transcript show: 'Show each element:'; cr.
oc do: [ :element | Transcript show: element; cr ].
Gives this output:
Show the collection:
an OrderedCollection(2 #(4 9))
Show each element:
2
#(4 9)