"Redo: Better printing with printOn:"
Quentin Ducasse.
-Hello, everyone.
In part 6 of the redo,
we will see how to change the output the "Print it" command gave us.
For now, we will look at the Counter object we created,
if we remove the "increment" and print everything.
So, double-click, right-click, Print it or we can simply press Cmd+P.
We can see that it is a counter
but we do not have much info on the value it holds
and this might be valuable.
In order to change that,
we have to take a look at the printOn method.
Let's go there and write the "printOn:"
so it takes "aStream" as an argument and...
We want to keep the Counter we had before.
So, this is a Counter.
But we want to add value.
We have to do : "aStream nextPutAll:".
And we have to add "withValue"
and in order to concatenate two strings, we will use the comma
and then write "count"
and convert for count to a string with "printString".
This is a counter,
this is "withValue",
and this will add the actual value of "count" to a string.
And now,
we will add a carriage return.
Let's save all this
and see how it works now.
We should have a Counter with value 0, right?
Let's print it.
"Counter withValue 0". That is great.
Now, if we change this, and make it start at 23,
and print this,
"Counter withValue 23". So, that is cool.
That is working as expected and it is a good time to commit what we changed.
Let's go to Commit,
say: "printOn method corrected
(with value)".
And let's commit everything.
So, in this part,
we learned how to change the output we can get with a "Print it" command
on an object we have defined.