truncate may leave trailing spaces... do you think this is wrong?

Hi all -

I noticed the other day that it's possible to truncate a string such that it has trailing spaces before it appends the omission characters. Notice the space in the output below.

ruby-1.8.7-p330 :007 > "one two three".truncate(7)
=> "one ..."

In my opinion, that space shouldn't be there.

I was going to submit tests and a patch to correct it, but was hoping to get some feedback to see if I'm wasting my time as people expect this (I can't think of a reason...).

Anyone have any thoughts on the matter?

-philip

I can imagine circumstances involving fixed-width fonts where having
an unpredictable number of characters left would be bad, and at the very
least a POLS violation.

And personally, the space being included doesn't bother me. :slight_smile:

FWIW!

ruby-1.8.7-p330 :007 > "one two three".truncate(7)
=> "one ..."

In my opinion, that space shouldn't be there.

Anyone have any thoughts on the matter?

I can imagine circumstances involving fixed-width fonts where having
an unpredictable number of characters left would be bad, and at the very
least a POLS violation.

That's a good case. I'd counter that if the widths are important you can't use truncate because if the string is shorter than you expect it won't be padded properly anyway. Still... worth considering.

I went ahead and wrote the patch and submitted it with comments about the change in behavior. I could always add a "strip" option to get what I want.

If anyone is interested...

https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/6357-patch-to-remove-trailing-spaces-from-truncate-result

And personally, the space being included doesn't bother me. :slight_smile:

Heh. It drives me crazy :slight_smile:

-philip

Philip Hallstrom wrote in post #978930:

I went ahead and wrote the patch and submitted it with comments about
the change in behavior. I could always add a "strip" option to get what
I want.

If anyone is interested...

https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/6357-patch-to-remove-trailing-spaces-from-truncate-result

This might be fine for English (or English like) strings, but not all
language necessarily separate words by space characters.

Truncate is not strip. It probably should not make this assumption.

Robert Walker wrote in post #978946:

Philip Hallstrom wrote in post #978930:

I went ahead and wrote the patch and submitted it with comments about
the change in behavior. I could always add a "strip" option to get what
I want.

If anyone is interested...

https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/6357-patch-to-remove-trailing-spaces-from-truncate-result

This might be fine for English (or English like) strings, but not all
language necessarily separate words by space characters.

Truncate is not strip. It probably should not make this assumption.

Oh! I forgot to mention that truncate already includes the :separator
option:

truncate("Once upon a time in a world far far away", :length => 17,
:separator => ' ')

Philip Hallstrom wrote in post #978930:

I went ahead and wrote the patch and submitted it with comments about
the change in behavior. I could always add a "strip" option to get what
I want.

If anyone is interested...

https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/6357-patch-to-remove-trailing-spaces-from-truncate-result

This might be fine for English (or English like) strings, but not all
language necessarily separate words by space characters.

I'm not sure that matters. I don't mind if "Hello World" gets truncated to "Hel...". I do mind (from an aesthetics point of view) "Hello ..."...

Truncate is not strip. It probably should not make this assumption.

Perhaps not. But there's no other way to clean that up which means I'd have to write my own truncate method which also seems wrong. :confused:

Oh! I forgot to mention that truncate already includes the :separator
option:

truncate("Once upon a time in a world far far away", :length => 17,
:separator => ' ')

That will force the break on a space. I don't necessarily want that either.

All valid though. We'll see what core says on the ticket. If they prefer I add a :strip => true option I'm fine with that as it gets me what I want without changing defaults...

-philip