How to print an integer to transcript

7.4k Views Asked by At

I've tried the code

|myNum|
myNum := SmallInteger new: 0.
Transcript show: (myNum printString).

, but Pharo crashes upon running this code.

2

There are 2 best solutions below

0
On BEST ANSWER

You can't create a SmallInteger using new:. You already have the number you want to print. It's already a SmallInteger. Just use it like such:

Transcript show: 0 printString
1
On

"SmallInteger new: 0" is not necessary since 0 is already a SmallInteger.

Besides, the #show: message of the Transcript object already sends #asString to its parameter, therefore it can receive any object.

So the correct code would be:

Transcript show: 0