String#tr_s

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

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

x.to_s # quite impressive, right? :wink:

and now look at this code

String === x ? x : x.to_s

what do you prefer?

HTH
Robert

···

On Jan 4, 2008 10:44 PM, Jordan Cooper <nefigah@gmail.com> 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://ruby-smalltalk.blogspot.com/

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

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

Jan

···

On Friday 04 January 2008 22:44:48 Jordan Cooper 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?

Robert Dober 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"

···

--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Jan Dvorak 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!

···

--
Posted via http://www.ruby-forum.com/.

Stupid me, I thought that was simply #tr, my bad.
R.

···

On Jan 4, 2008 11:12 PM, Sebastian Hungerecker <sepp2k@googlemail.com> wrote:

Robert Dober wrote:
> I suppose you mean to_s.

No, he doesn't.

# 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 Wright

···

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

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