Re: Array#to_s in Ruby 1.9 Head

From: [email protected] [mailto:[email protected]]

Mauricio F. writes in http://eigenclass.org/hiki.rb?Changes+in
+Ruby+1.9+update+5
that Ruby 1.9 Head has changed Array#to_s to be an alias for
Array#to_inspect.

What is the motivation for that change?
print a
becomes
a.each { |x| print x }

Or (easier):
print a.join(’’)

On Oct 19, 2006, at 6:05 PM, Gavin K. wrote:

Or (easier):
print a.join(’’)

Sure. But both solutions fail with nested arrays.

I still prefer the 1.8 default behavior.

Gary W.

Gavin K. wrote:

What is the motivation for that change?
print a
becomes
a.each { |x| print x }

Or (easier):
print a.join(’’)

For less typing, omit the string to
print a.join

David V.

Liquid depends on the current Array#to_s for speed. It builds nested
array of objects which all render their final result upon the to_s
call.

On Oct 19, 2006, at 9:45 PM, Trans wrote:

Tobias Lütke wrote:

Liquid depends on the current Array#to_s for speed. It builds nested
array of objects which all render their final result upon the to_s
call.

Perhaps then:

print a.flatten.join

Yeah, sure, there are lots of ways to work around it but why should
Array#to_s default to generating debugging output? That is what
Array#inspect is for, right?

Gary W.

Tobias Lütke wrote:

Liquid depends on the current Array#to_s for speed. It builds nested
array of objects which all render their final result upon the to_s
call.

Perhaps then:

print a.flatten.join

T.

Hi,

In message “Re: Array#to_s in Ruby 1.9 Head”
on Fri, 20 Oct 2006 10:50:00 +0900, [email protected] writes:

|Yeah, sure, there are lots of ways to work around it but why should
|Array#to_s default to generating debugging output? That is what
|Array#inspect is for, right?

The official purpose of #to_s method is that returning string
representation of the object. No more, no less. So it’s up to our
expectation for the string representation of the object. I changed
the behavior to make string representation of arrays more
distinguishable from other objects.

I don’t think to_s SHOULD work as replacement of Array#join. But
there may be another reason I missed to keep it compatible with 1.8.

						matz.

On Oct 20, 2006, at 12:57 AM, Yukihiro M. wrote:

The official purpose of #to_s method is that returning string
representation of the object. No more, no less. So it’s up to our
expectation for the string representation of the object. I changed
the behavior to make string representation of arrays more
distinguishable from other objects.

I don’t think to_s SHOULD work as replacement of Array#join. But
there may be another reason I missed to keep it compatible with 1.8.

OK, I grabbed ruby 1.9 so that I could actually play with it.

I always assumed that Array#join simply called Array#to_s on each
element
and then constructed the final string by inserting the separator between
each element, but it actually seems to flatten the array first:

Ruby 1.8:
a = [1,2]
b = [3,4]
c = [a,‘foo’,b]
c.to_s # 12foo34
c.inspect # [[1, 2], “foo”, [3, 4]]
c.join # 12foo34
c.join(’-’) # 1-2-foo-3-4

When the separator is the empty string, the two algorithms are the
same, but
if the separator is not empty then they give different results. The
non-flattening algorithm would produce: 12-foo-34 in the above example.

My objections to the Array#to_s behavior in 1.9 were based on
thinking about
Array#join as simply calling #to_s on each element (without the
flattening step),
which would make it more difficult to work around.

So in 1.9, the above example produces:
c.to_s # [[1, 2], “foo”, [3, 4]]
c.inspect # [[1, 2], “foo”, [3, 4]]
c.join # 12foo34
c.join(’-’) # 1-2-foo-3-4

I think the documentation for Array#join should mention the
flattening step.
Something like:

  Returns a string created by flattening the array, converting

each element of
the result to a string via #to_s, and concatenating the strings
with sep as
the separator.

Gary W.

Hi,

In message “Re: Array#to_s in Ruby 1.9 Head”
on Fri, 20 Oct 2006 10:19:54 +0900,
“=?ISO-8859-1?Q?Tobias_L=FCtke?=” [email protected] writes:
|
|Liquid depends on the current Array#to_s for speed. It builds nested
|array of objects which all render their final result upon the to_s
|call.

Do you mean Array#join does not work for you this case?

						matz.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs