Philip Hallstrom
2011-Feb-01 03:31 UTC
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hassan Schroeder
2011-Feb-01 18:40 UTC
Re: truncate may leave trailing spaces... do you think this is wrong?
On Mon, Jan 31, 2011 at 7:31 PM, Philip Hallstrom <philip-LSG90OXdqQE@public.gmane.org> wrote:> 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. And personally, the space being included doesn''t bother me. :-) FWIW! -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Philip Hallstrom
2011-Feb-01 19:08 UTC
Re: truncate may leave trailing spaces... do you think this is wrong?
>> 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. :-)Heh. It drives me crazy :) -philip -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Walker
2011-Feb-01 20:06 UTC
Re: truncate may leave trailing spaces do you think this is wrong?
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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Walker
2011-Feb-01 20:09 UTC
Re: truncate may leave trailing spaces do you think this is wrong?
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 => '' '') -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Philip Hallstrom
2011-Feb-01 21:34 UTC
Re: Re: truncate may leave trailing spaces do you think this is wrong?
>> 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. :/> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.