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?

On Jan 4, 2008 10:44 PM, Jordan C. [email protected] 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/.

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


http://ruby-smalltalk.blogspot.com/


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

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

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!

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’, '’) #=> "ho"
“hello”.tr_s(‘el’, ‘hx’) #=> “hhxo”

On Jan 4, 2008 11:12 PM, Sebastian H. [email protected]
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.

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs