The default array print format

Hello,

I was playing around with some LDAP results in a Rails system and it did
not
work at all, I kept getting strang — and - in the results. It turned
out
I did not index the array properly and I was converting it to strings…
Another thing I noticed lately is that when I try to print arrays they
don’t
look as good as in say PHP.
Now I wonder: isn’t it possible to make array print nicer and clearer by
default? I know there is a pretty print package, but why stick to a less
clear default implementation?
Just wondering of course…

Bart

On 20.09.2006 15:07, Bart B. wrote:

Just wondering of course…
Did you probably miss one of the standard methods available?

a=%w{foo bar baz}
=> [“foo”, “bar”, “baz”]

puts a
foo
bar
baz
=> nil

p a
[“foo”, “bar”, “baz”]
=> nil

puts a.to_s
foobarbaz
=> nil

puts a.inspect
[“foo”, “bar”, “baz”]
=> nil

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?

Kind regards

robert

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
to_s
method. And I don’t understand why that remove-all-spaces approach is
chosen. But perhaps there are good reasons for this, I’m not a longtime
ruby user so I just wonder.

Thanks for your explanation
Bart

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
to_s method.

I have no insight at which exact point it does this. But I guess you
can
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
all
its elements. It’s like Array#join called without arguments (or with an
empty string as argument). :slight_smile:

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
simplest
thing possible (i.e. converting all elements to string via their #to_s
and
then concatenate them). If the user wants something more fancy, she has
to
explicitly use another method, e.g. join with an argument that denotes
the
string that is inserted in between.

Thanks for your explanation
Bart

You’re welcome!

Kind regards

robert

Robert K. wrote:

to_s method.
spaces in an array, Array#to_s just appends string representations of
more fancy, she has to explicitly use another method, e.g. join with an
argument that denotes the string that is inserted in between.

You can just set $, to the separator you want.

$ ruby -e ‘$,=","; puts [1,2,3,4].to_s’
1,2,3,4

It’s also the default argument to #join.