[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

String#tr_s

J. Cooper

1/4/2008 9:45:00 PM

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

6 Answers

Robert Dober

1/4/2008 9:54:00 PM

0

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

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

Jan Dvorak

1/4/2008 10:07:00 PM

0

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?

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

Jan

Sebastian Hungerecker

1/4/2008 10:12:00 PM

0

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

J. Cooper

1/4/2008 10:51:00 PM

0

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

Robert Dober

1/4/2008 11:29:00 PM

0

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.
Stupid me, I thought that was simply #tr, my bad.
R.

Gary Wright

1/4/2008 11:41:00 PM

0


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 Wright