Why? puts and p do the same thing, it feels redundant. The only reason I can think of, is it’s consistent with puts(Tuple) and puts(Hash), but it’s unexpected (after Ruby) and useless (duplicate of p).
Ruby’s behavior is convenient, I sometimes use it to puts caller. Then sometimes I forget about it and find it inconvenient and I have to switch to p.
In Ruby, puts array is a special case. In Crystal we try to avoid special cases. That’s why I didn’t copy this strange behavior.
What would happen for other container types? Would they need to somehow change the behavior of puts? Why is array special? That’s why I didn’t copy it.
Greeting, strangers!
How do you feel about puts(Array) printing items one by one instead of emulating p(Array) as it is now? Would that be confusing for you?
I’m using Crystal from 2014 so my opinion will be biased,
but I think that p(Array) should print the array without a new line, and if you want a multi-line display, pp(Array) should give you that.
I’ve come from Pascal and C (but also have some experience with Ruby) and i think puts(Array) behaving differently from puts(Tuple) would be very confusing, to a point of never using puts on something except String (so I would always do puts(x.to_s)).
I would be unhappy with the change because it seems unnecessary and behaves differently from any other language I’ve worked with (since I have very little Ruby experience). I agree with others here that Array should behave similarly to Tuple and other Enumerable types when used with puts.
I also don’t think that it’s a bad thing that it takes more code to print array elements on multiple lines. While the array.each method you showed above is more code than just puts, it’s much more clear what’s intended. One of Crystal’s greatest strengths is that it’s naturally pretty easy to read, and I think that encouraging self-documenting code like that is more important than giving shorter ways to do everything.
To be honest, when I know I’m dealing with an array, I use puts array in Ruby if I want to print elements line by line. In Crystal I need to do puts array.join("\n") which is a bit longer.
The thing is, it took me a while to understand why Ruby was doing this strange thing with puts with Array. And from time to time it annoys me that I want to print an array like what you normally get when you puts a Hash or basically anything, and Ruby prints it line by line. Then I have to change it to p.
In Crystal you have to type a bit more for the “print each item in a line”, but I prefer consistency over special cases.