[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Array#to_s in Ruby 1.9 Head

Gavin Kistner

10/19/2006 10:06:00 PM

From: gwtmp01@mac.com [mailto:gwtmp01@mac.com]
> Mauricio Fernandez writes in http://eigenclass.org/hiki.rb?...
> +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('')

8 Answers

Gary Wright

10/19/2006 10:35:00 PM

0


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

> From: gwtmp01@mac.com [mailto:gwtmp01@mac.com]
>> Mauricio Fernandez writes in http://eigenclass.org/hiki.rb?...
>> +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('')

Sure. But both solutions fail with nested arrays.

I still prefer the 1.8 default behavior.


Gary Wright




David Vallner

10/20/2006 12:57:00 AM

0

Gavin Kistner 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 Vallner


Tobias Luetke

10/20/2006 1:20:00 AM

0

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 10/19/06, David Vallner <david@vallner.net> wrote:
> Gavin Kistner 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 Vallner
>
>
>
>
>


--
Tobi
http://s... - modern e-commerce software
http://typo.le... - Open source weblog engine
http://blog.le... - Technical weblog

Trans

10/20/2006 1:42:00 AM

0


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.

Gary Wright

10/20/2006 1:50:00 AM

0


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 Wright




Yukihiro Matsumoto

10/20/2006 4:46:00 AM

0

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?=" <tobias.luetke@gmail.com> 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.

Yukihiro Matsumoto

10/20/2006 4:57:00 AM

0

Hi,

In message "Re: Array#to_s in Ruby 1.9 Head"
on Fri, 20 Oct 2006 10:50:00 +0900, gwtmp01@mac.com 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.

Gary Wright

10/21/2006 9:05:00 PM

0


On Oct 20, 2006, at 12:57 AM, Yukihiro Matsumoto 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 Wright