# Triming string to fit a specific width

**URL:** <https://www.ruby-forum.com/t/triming-string-to-fit-a-specific-width/72828>\
**Category:** Rails\
**Created:** [October 19, 2006, 7:35am UTC](https://www.ruby-forum.com/t/triming-string-to-fit-a-specific-width/72828 "2006-10-19T07:35:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![femto\_gary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ba8739/32.png) [@femto\_gary](https://www.ruby-forum.com/u/femto_gary)\
**Post date:** [October 19, 2006, 7:35am UTC](https://www.ruby-forum.com/t/triming-string-to-fit-a-specific-width/72828/1 "2006-10-19T07:35:50Z")

</div>

hello all, I have a requirement to trim a string to fit  
a specific width. This data is displayed under some table column,  
and need to fit the column width. Since I can’t tell the exact pixel in  
html.  
so we needn’t be too exact about the trimLength. I want to do something  
like  
trimString(String source, int hintColumnWidth), but can I Unit Test it?(  
since  
the requirement is vague here.)  
Also the requirement says that , when it trims, it should display full  
string  
as tooltip , and that becomes etc, and then can I UT  
it?  
Thanks.

---

<div class="post-metadata">

**Author:** ![femto\_gary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ba8739/32.png) [@femto\_gary](https://www.ruby-forum.com/u/femto_gary)\
**Post date:** [October 19, 2006, 7:59am UTC](https://www.ruby-forum.com/t/triming-string-to-fit-a-specific-width/72828/2 "2006-10-19T07:59:53Z")

</div>

On 10/19/06, femto gary [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the requirement is vague here.)  
> Also the requirement says that , when it trims, it should display full  
> string  
> as tooltip , and that becomes etc, and then can I UT it?  
> Thanks.

trucate will trucate your text, but you will need a helper to do the  
rest.  
Something like

def my\_cell\_value( a\_string )  
“\<span title='#{a\_string}\>#{truncate( a\_string, 23 )}”  
end

in your helper file should do the job.

Hope that helps

---

<div class="post-metadata">

**Author:** ![femto\_gary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ba8739/32.png) [@femto\_gary](https://www.ruby-forum.com/u/femto_gary)\
**Post date:** [October 19, 2006, 8:32am UTC](https://www.ruby-forum.com/t/triming-string-to-fit-a-specific-width/72828/3 "2006-10-19T08:32:03Z")

</div>

Thanks Daniel ,  
I know the general code.  
What I want to know is about testing.  
Since the requirement isn’t very exact,  
so say, 3 columnWidthUnit = 1 strLengthUnit,  
so I write trim(str,columnWidthUnit/3) for that.  
so if the method is trimString(source, hintColumnWidth),  
Then in test, I just test trimString(“helloworld”, 3) produces “h…”,  
Am I correct?  
And about tooltip, I test result str equals to “h…”,  
right? So If I will put in html other attributes, like ,  
then I use regex to test it? Right?

On 10/19/06, Daniel N [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> like
> 
> >

–  
Best Regards  
femto [http://femto.blogdriver.com](http://femto.blogdriver.com)

---

<div class="post-metadata">

**Author:** ![femto\_gary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ba8739/32.png) [@femto\_gary](https://www.ruby-forum.com/u/femto_gary)\
**Post date:** [October 19, 2006, 8:50am UTC](https://www.ruby-forum.com/t/triming-string-to-fit-a-specific-width/72828/4 "2006-10-19T08:50:09Z")

</div>

On 10/19/06, femto gary [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Am I correct?  
> And about tooltip, I test result str equals to “h…”,  
> right? So If I will put in html other attributes, like ,  
> then I use regex to test it? Right?

Sorri . Seems I misunderstood.

I wouldn’t use a regex directly for this. You can use something like  
assert\_tag or even better HPricott

[http://api.rubyonrails.org/classes/Test/Unit/Assertions.html#M000970](http://api.rubyonrails.org/classes/Test/Unit/Assertions.html#M000970)

Luck Redpath has a small writeup on using HPricott to test views. It’s  
here

[http://www.lukeredpath.co.uk/2006/7/7/testing-your-rails-views-with-hpricot](http://www.lukeredpath.co.uk/2006/7/7/testing-your-rails-views-with-hpricot)

I hope that’s more along the lines of what your after.

---

<div class="post-metadata">

**Author:** ![femto\_gary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ba8739/32.png) [@femto\_gary](https://www.ruby-forum.com/u/femto_gary)\
**Post date:** [October 19, 2006, 9:06am UTC](https://www.ruby-forum.com/t/triming-string-to-fit-a-specific-width/72828/5 "2006-10-19T09:06:10Z")

</div>

Hi Daniel, that looks interesting.I’ll check it up.  
I’d thought about selenium, but I feel  
in this small requirement using selenium  
to test that would be an overkill and too much  
effort to set up. Thanks for the hint.

On 10/19/06, Daniel N [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > so if the method is trimString(source, hintColumnWidth),  
> > I wouldn’t use a regex directly for this. You can use something like
> 
> >

–  
Best Regards  
femto [http://femto.blogdriver.com](http://femto.blogdriver.com)
