Crystal: Always make puts append a newline at the end of a string

Created on 16 Apr 2019  路  9Comments  路  Source: crystal-lang/crystal

I think a string printed using puts should always be followed by a newline (i.e. the unless should be removed in src/io.cr).

Why is no newline printed when the string already ends with one? Just because Ruby does it like this? It doesn't make sense to me.

Before I didn't know that this is completely intentionally done, I always wondered why this happens. Why do I need to write two \n at the end of my string to get one newline? That was always very confusing to me and sometimes made me think there was in a bug in my terminal or something.
To me this is just unnecessary logic on such essential methods such as puts.
This also leads to this rather confusing behavior:

puts "hello\n":


print "hello\n":


(Why do puts and print both do the same here? Aren't they different methods?) is what the
thoughts of a beginner could be to this for example.

This behavior also goes a little bit into the direction of fixing bad code.
It might happen that somehow an additional newline got at the end of your string even if it shouldn't be there and then you print that string with puts and think: Perfect, everything works fine! (Even if it doesn't).

Most helpful comment

I like the current behavior. Having to check whether a string has a linefeed or not, or having a double linefeed all of a sudden sounds bothersome and boring.

All 9 comments

Ruby does that, I copied it from Ruby.

I think the reason is that if you do:

puts gets

because gets returns a newline it would be strange to see two newlines there. Well, gets used to return a newline but now it doesn't unless you pass chomp: false, so we might want to revert this behavior.

(but I'm not sure what the problem with this behavior is?)

I like the current behavior. Having to check whether a string has a linefeed or not, or having a double linefeed all of a sudden sounds bothersome and boring.

The problem is that this behaviour is not exactly what you would expect. Maybe this might be acceptable for developers working directly with the stdlib API putting strings on an IO. But this is also relevant for deferred use cases. For example people writing translations. When a string "foo" prints "foo" and a line break, you would expect "foo\n" to print "foo" and two line breaks. This is far away from specifics of IO.puts. IMO it should be simple and expectable.

My only observation is that this works like that in Ruby but it seems nobody knew this. So if you all used Ruby in the past and didn't notice it, then it might not be a problem. Just a guess, though.

Yeah, I think nobody cares because puts is mostly used for printing quick debug messages with a linefeed.

@ysbaddaden For ::puts this is true. But IO#puts is used for printing all kinds of stuff on an IO.

It's definitely an edge case, though.

So if you all used Ruby in the past and didn't notice it, then it might not be a problem.

I think I did notice it in Ruby too. But it was pretty much my first language and I think back then I didn't dare to insinuate that there was something wrong with a programming language and then I pretty much ignored it. But now afterwards it does bother me and it doesn't make sense to me.
In this language we can do it better and remove this behavior, especially because puts gets does what you want without it.

Is there any update on this?

There are 6 people for this, the people that reacted with 馃憤 on the proposal and myself and there are 5 comments for this.
There are 5 people against this, the people that reacted with 馃憤 on ysbaddaden's comment and ysbaddaden himself and there are 3 comments against this.
asterite seems to be somewhat in the middle (not exactly on one side I assume).
Overall there are more arguments in all the comments for it than against it.

Does that mean this is accepted or should there be a final voting maybe?

I vote to keep the current behaviour.

puts guarantees to finish with a new line. It's simple effort to avoid double new lines.
Using IO for machine friend format will probably not have its own format avoiding the use of puts.

Was this page helpful?
0 / 5 - 0 ratings