# String#tr\_s

**URL:** <https://www.ruby-forum.com/t/string-tr-s/125062>\
**Category:** Ruby\
**Created:** [January 4, 2008, 10:44pm UTC](https://www.ruby-forum.com/t/string-tr-s/125062 "2008-01-04T22:44:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nefigah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@nefigah](https://www.ruby-forum.com/u/nefigah)\
**Post date:** [January 4, 2008, 10:44pm UTC](https://www.ruby-forum.com/t/string-tr-s/125062/1 "2008-01-04T22:44:53Z")

</div>

This seems to be a pretty good example of a “built-in” function that I  
can’t really see a practical use for… why does it need to be standard?  
Why is it named so cryptically? When would you ever want to use it?

---

<div class="post-metadata">

**Author:** ![nefigah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@nefigah](https://www.ruby-forum.com/u/nefigah)\
**Post date:** [January 4, 2008, 10:54pm UTC](https://www.ruby-forum.com/t/string-tr-s/125062/2 "2008-01-04T22:54:51Z")

</div>

On Jan 4, 2008 10:44 PM, Jordan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## This seems to be a pretty good example of a “built-in” function that I can’t really see a practical use for… why does it need to be standard? Why is it named so cryptically? When would you ever want to use it?
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

I suppose you mean to\_s.  
Well this is a case of second sight ;). Look at this code

x.to\_s # quite impressive, right? 😉

and now look at this code

String === x ? x : x.to\_s

what do you prefer?

HTH  
Robert

–  
[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

Whereof one cannot speak, thereof one must be silent.  
Ludwig Wittgenstein

---

<div class="post-metadata">

**Author:** ![nefigah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@nefigah](https://www.ruby-forum.com/u/nefigah)\
**Post date:** [January 4, 2008, 11:07pm UTC](https://www.ruby-forum.com/t/string-tr-s/125062/3 "2008-01-04T23:07:43Z")

</div>

On Friday 04 January 2008 22:44:48 Jordan C. wrote:

> This seems to be a pretty good example of a “built-in” function that I  
> can’t really see a practical use for… why does it need to be standard?  
> Why is it named so cryptically? When would you ever want to use it?

It’s named after the unix program ‘tr’ with parameter ‘-s’ as in  
squeeze.

Jan

---

<div class="post-metadata">

**Author:** ![nefigah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@nefigah](https://www.ruby-forum.com/u/nefigah)\
**Post date:** [January 4, 2008, 11:51pm UTC](https://www.ruby-forum.com/t/string-tr-s/125062/4 "2008-01-04T23:51:30Z")

</div>

Jan D. wrote:

> It’s named after the unix program ‘tr’ with parameter ‘-s’ as in  
> squeeze.
> 
> Jan

That makes more sense, thank you. I’m still not sure if it’s justified,  
in my head; but I guess that may be because of my C# background, where  
it’s encouraged to err on the side of too verbose as opposed to too  
cryptic.

Unfortunately I still don’t see a practical use for the thing though.

Thanks!

---

<div class="post-metadata">

**Author:** ![nefigah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@nefigah](https://www.ruby-forum.com/u/nefigah)\
**Post date:** [January 4, 2008, 11:12pm UTC](https://www.ruby-forum.com/t/string-tr-s/125062/5 "2008-01-04T23:12:58Z")

</div>

Robert D. wrote:

> I suppose you mean to\_s.

No, he doesn’t.

str.tr\_s(from\_str, to\_str) =\> new\_str  
Processes a copy of _str_ as described under +String#tr+, then  
removes duplicate characters in regions that were affected by the  
translation.  
“hello”.tr\_s(‘l’, ‘r’) #=\> “hero”  
“hello”.tr\_s(‘el’, '_’) #=\> "h_o"  
“hello”.tr\_s(‘el’, ‘hx’) #=\> “hhxo”

---

<div class="post-metadata">

**Author:** ![nefigah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@nefigah](https://www.ruby-forum.com/u/nefigah)\
**Post date:** [January 5, 2008, 12:29am UTC](https://www.ruby-forum.com/t/string-tr-s/125062/6 "2008-01-05T00:29:07Z")

</div>

On Jan 4, 2008 11:12 PM, Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Robert D. wrote:
> 
> > I suppose you mean to\_s.
> 
> No, he doesn’t.  
> Stupid me, I thought that was simply #tr, my bad.  
> R.

---

<div class="post-metadata">

**Author:** ![nefigah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@nefigah](https://www.ruby-forum.com/u/nefigah)\
**Post date:** [January 5, 2008, 12:41am UTC](https://www.ruby-forum.com/t/string-tr-s/125062/7 "2008-01-05T00:41:59Z")

</div>

On Jan 4, 2008, at 5:51 PM, J. Cooper wrote:

> Unfortunately I still don’t see a practical use for the thing though.

# Remove blank lines from standard input

ruby -e ‘puts ARGF.read.tr\_s("\n", “\n”)’

# convert the standard input into a list of words, one per line:

ruby -e ‘puts ARGF.read.tr\_s("^A-Za-z", “\n”)’

Gary W.
