Search code examples
smalltalkpharo

Stream assigment in pharo


I have a problem here.

I have a data variable of type an OrderedCollection.

this variable shows me this when I do a DoIt: an OrderedCollection ('3c7lwmdva2b8jbt39ls4pz3sl' '3c7lwmbf36tamw1m45riirdze' 8 February 1994).

Now I would like this:

object:=String streamContents:
    [:stream|
    stream
    nextPutAll: 'data:= ';cr;
    print:data asArray.]

But when I run, it shows me this:

data := an Array End of statement list encounteencountered -> ('3c7lwmdva2b8jbt39ls4pz3sl' '3c7lwmbf36tamw1m45riirdze' 8 February 1994).

So month I wanted to get this:

data := #('3c7lwmdva2b8jbt39ls4pz3sl' '3c7lwmbf36tamw1m45riirdze' 8 February 1994).

How to do please?


Solution

  • result := String streamContents: [:stream |
      stream nextPutAll: 'data := #('.
      data
        do: [:string | stream nextPut: $'; nextPutAll: string asString; nextPut: $']
        separatedBy: [stream space].
      stream nextPut: $)]
    

    Since my answer has been downvoted, I'll explain the solution.

    1. What's in data? The question says that data prints as (original formatting, sorry about that)
    an OrderedCollection ('3c7lwmdva2b8jbt39ls4pz3sl' '3c7lwmbf36tamw1m45riirdze' 8 February 1994).
    

    which indicates that data is an OrderedCollection with two strings and a Date.

    1. What is the OP trying to compute? It is not clear. The use of String streamContents: seems to indicate that the OP is trying to produce a String, more precisely an assignment sentence where data is assigned the OrderedCollection converted to an Array.

    2. Solution to 2. Assuming my guess in 2 is right, my code above produces such a sentence.

    3. What other interpretation can we give to this unclear question? Well, may be the OP is just looking for a method that would convert the OrderedCollection into an Array. In this case, the answer would have been simply

    object := data asArray.
    

    However, given a previous post, where the same OP was trying to do some metaprogramming, the actual intention remains unclear.