Bart B. [email protected] wrote:
Robert K. wrote:
Granted, the version using to_s is certainly questionable but the
other two alternatives (especially p, which uses #inspect
internally) are pretty clear, aren’t they?
They sure are, but some projects, in this case Rails, seem to use the
I have no insight at which exact point it does this. But I guess you
change that, or explicitly invoke another method.
And I don’t understand why that remove-all-spaces
approach is chosen.
Probably because it is not a “remove all spaces” approach: there are no
spaces in an array, Array#to_s just appends string representations of
its elements. It’s like Array#join called without arguments (or with an
empty string as argument).
But perhaps there are good reasons for this, I’m
not a longtime ruby user so I just wonder.
My guess at the reasoning behind this goes like this: to_s does the
thing possible (i.e. converting all elements to string via their #to_s
then concatenate them). If the user wants something more fancy, she has
explicitly use another method, e.g. join with an argument that denotes
string that is inserted in between.
Thanks for your explanation