The default array print format

B

Bart Braem

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
 
R

Robert Klemme

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

Did you probably miss one of the standard methods available?
a=%w{foo bar baz} => ["foo", "bar", "baz"]
puts a
foo
bar
baz
=> nil["foo", "bar", "baz"]
=> nilfoobarbaz
=> nil["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
 
B

Bart Braem

Robert said:
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
 
R

Robert Klemme

Bart Braem said:
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). :)
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
 
C

Carlos

Robert said:
I have no insight at which exact point it does this. But I guess you
can change that, or explicitly invoke another method.



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). :)



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.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top