# Confusion about to\_s

**URL:** <https://forum.crystal-lang.org/t/confusion-about-to-s/6275>\
**Category:** Help & Support\
**Created:** [January 8, 2024, 4:19am UTC](https://forum.crystal-lang.org/t/confusion-about-to-s/6275 "2024-01-08T04:19:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![femto](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/femto/32/2246_2.png) [@femto](https://forum.crystal-lang.org/u/femto)\
**Post date:** [January 8, 2024, 4:19am UTC](https://forum.crystal-lang.org/t/confusion-about-to-s/6275/1 "2024-01-08T04:19:07Z")

</div>

while reading asterite’s blog post about incremental-compilation  
[Incremental compilation for Crystal - Part 3 - DEV Community](https://dev.to/asterite/incremental-compilation-for-crystal-part-2-3184),

it says io.cr has dependencies on foo.  
A piece that puzzles me is why crystal choose this protocol of to\_s(io) ?  
in ruby you define to\_s

```auto
def to_s
  ...
end

```

if some io wants to use it  
then just io \<\< xxx.to\_s, which is simpler

in python is similar,

```auto
def __str__ (self):
   ...

```

why crystal choose this protocol?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [January 8, 2024, 4:38am UTC](https://forum.crystal-lang.org/t/confusion-about-to-s/6275/2 "2024-01-08T04:38:22Z")

</div>

There’s a section in the reference book about this: [Performance - Crystal](https://crystal-lang.org/reference/1.10/guides/performance.html#dont-create-intermediate-strings-when-writing-to-an-io)

> This philosophy of appending to an IO instead of returning an intermediate string results in better performance than handling intermediate strings.

EDIT: You _can_ however still override `def to_s : String` instead of the `IO` version. This can be useful if you already have the string representation in memory (such as within an ivar) to save some additional memory.
