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