Forum: Ruby Question regarding tr method in Strings Class

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ee04bc0ca6dcdad4a7e8a8e1d4efb5d0?d=identicon&s=25 Michael W. Ryder (Guest)
on 2007-05-03 06:30
(Received via mailing list)
Is there a way to denote a null in the replacement for a character using
this method?  For example, if I have a = "Jones,Ja'me" and I want to
convert the comma to a space and remove the apostrophe without placing a
character in there.  I know I can use a.tr(",", " ").tr("'", "") to get
the desired result but would prefer to be able to say something like
a.tr(",'", " ") and have it remove the apostrophe and replace the comma
in one step.  Besides, if I am going to be removing a lot of punctuation
the string could get very long and complicated.
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-05-03 06:42
(Received via mailing list)
On 5/3/07, Michael W. Ryder <_mwryder@worldnet.att.net> wrote:
> Is there a way to denote a null in the replacement for a character using
> this method?  For example, if I have a = "Jones,Ja'me" and I want to
> convert the comma to a space and remove the apostrophe without placing a
> character in there.  I know I can use a.tr(",", " ").tr("'", "") to get
> the desired result but would prefer to be able to say something like
> a.tr(",'", " ") and have it remove the apostrophe and replace the comma
> in one step.  Besides, if I am going to be removing a lot of punctuation
> the string could get very long and complicated.
>
I do not think this works but why not use the "right" tool for each task
tr(","," ").gsub(/'/,"")
does not look too clumsy to me ;)

Cheers
Robert
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2007-05-03 06:58
(Received via mailing list)
From: Michael W. Ryder [mailto:_mwryder@worldnet.att.net]
# in one step.  Besides, if I am going to be removing a lot of
# punctuation the string could get very long and complicated.

delete and translate would do great

irb(main):001:0> "aasdfef".tr "ae","z"
=> "zzsdfzf"
irb(main):002:0> "aasdfef".tr "ae","zy"
=> "zzsdfyf"
irb(main):003:0> "aasdfef".delete "ae"
=> "sdff"
Ee04bc0ca6dcdad4a7e8a8e1d4efb5d0?d=identicon&s=25 Michael W. Ryder (Guest)
on 2007-05-03 07:15
(Received via mailing list)
Robert Dober wrote:
> I do not think this works but why not use the "right" tool for each task
> tr(","," ").gsub(/'/,"")
> does not look too clumsy to me ;)
>
> Cheers
> Robert
>
>
Your solution is about the same as the solution I found "clumsy".  I can
use a.tr to remove multiple different characters such as: a ="This is a
test, this is only a test!". a.tr(",!", "") will remove the comma and
exclamation mark.  So why can't I use the same method to remove the
comma and replace the exclamation mark with a period?  I know that there
are escape codes for things like tabs and new-lines, so I was wondering
if there was one for a null character.
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-05-03 07:21
(Received via mailing list)
On 5/3/07, Michael W. Ryder <_mwryder@worldnet.att.net> wrote:
<cut>
> Your solution is about the same as the solution I found "clumsy".
You are not coming from a Unix background, are you? We are used to
pasting things together like that, but I respect your taste.
 > I can
> use a.tr to remove multiple different characters such as: a ="This is a
> test, this is only a test!". a.tr(",!", "") will remove the comma and
> exclamation mark.  So why can't I use the same method to remove the
> comma and replace the exclamation mark with a period?  I know that there
> are escape codes for things like tabs and new-lines, so I was wondering
> if there was one for a null character.
I understood that you wanted to delete a character not to replace it
with a null character, which should be possible via \000. But I fail
to see the purpose of this in the context of your question.
Robert
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (Guest)
on 2007-05-03 09:41
(Received via mailing list)
On Thu, May 03, 2007 at 01:30:06PM +0900, Michael W. Ryder wrote:
> Is there a way to denote a null in the replacement for a character using
> this method?  For example, if I have a = "Jones,Ja'me" and I want to
> convert the comma to a space and remove the apostrophe without placing a
> character in there.  I know I can use a.tr(",", " ").tr("'", "") to get
> the desired result but would prefer to be able to say something like
> a.tr(",'", " ") and have it remove the apostrophe and replace the comma
> in one step.  Besides, if I am going to be removing a lot of punctuation
> the string could get very long and complicated.

Removing the apostrophe, and replacing it with null, are two different
things. But you could use the null as a placeholder and then remove it
afterwards:

a = "Jones,Ja'me"
a.tr(",'"," \000").delete("\000")

No matter how much punctuation you want to remove, it's still only two
steps.

If you want this in a single step, hmm, how about:

REPLACE = {
  "," => " ",
  "'" => "",
}
REPLACE_RE = /#{REPLACE.keys.map {|k| Regexp.escape(k) }.join("|")}/

a.gsub(REPLACE_RE) { |m| REPLACE[m] }

It might look a bit long-winded, but the constant initialisation only
has to
be done once at the top of your program, and it can replace arbitary
sequences of characters with other arbitary sequences.

HTH,

Brian.
Ee04bc0ca6dcdad4a7e8a8e1d4efb5d0?d=identicon&s=25 Michael W. Ryder (Guest)
on 2007-05-03 21:06
(Received via mailing list)
Robert Dober wrote:
>> are escape codes for things like tabs and new-lines, so I was wondering
>> if there was one for a null character.
> I understood that you wanted to delete a character not to replace it
> with a null character, which should be possible via \000. But I fail
> to see the purpose of this in the context of your question.
> Robert
>>
>>
>
>
I find your sig very appropriate.  I can find several ways to clean up a
string using two or more operations, I was just looking to see if there
was not also a way to do the same thing with one operation.
In some ways Ruby is far easier to use than the language I have been
using for over 25 years, but there are other places it is much harder.
I am doing a lot of experimenting to find out the best way for me to do
things and this is the latest experiment.
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2007-05-04 15:37
(Received via mailing list)
On 5/3/07, Michael W. Ryder <_mwryder@worldnet.att.net> wrote:


> In some ways Ruby is far easier to use than the language I have been
> using for over 25 years, but there are other places it is much harder.

Just out of curiosity, what language is that, and how does it make
this particular operation much easier than Ruby does?

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2007-05-04 15:47
(Received via mailing list)
On 5/3/07, Brian Candler <B.Candler@pobox.com> wrote:
>
>
> If you want this in a single step, hmm, how about:
>
> REPLACE = {
>         "," => " ",
>         "'" => "",
> }
> REPLACE_RE = /#{REPLACE.keys.map {|k| Regexp.escape(k) }.join("|")}/


Tweak:
  REPLACE_RE = Regexp.union(*REPLACE.keys)
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (Guest)
on 2007-05-04 20:34
(Received via mailing list)
On Fri, May 04, 2007 at 10:47:15PM +0900, Logan Capaldo wrote:
>
>
> Tweak:
>  REPLACE_RE = Regexp.union(*REPLACE.keys)

Ooh, shiny.
Ee04bc0ca6dcdad4a7e8a8e1d4efb5d0?d=identicon&s=25 Michael W. Ryder (Guest)
on 2007-05-04 21:01
(Received via mailing list)
Rick DeNatale wrote:
> On 5/3/07, Michael W. Ryder <_mwryder@worldnet.att.net> wrote:
>
>
>> In some ways Ruby is far easier to use than the language I have been
>> using for over 25 years, but there are other places it is much harder.
>
> Just out of curiosity, what language is that, and how does it make
> this particular operation much easier than Ruby does?
>

I have been using Business Basic professionally for over 25 years after
learning Fortran and Basic in college.  String handling is much easier
in Ruby once I get past the small differences, but in screen displays,
file handling, and exception handling Ruby is not even on the horizon.
These are all built into the language and are very easy to use.  As an
example, that I am working on in another thread, to print $12,345.67 in
Business Basic in the middle of a screen I enter 'Print
@(20,20),A:"$##,###,##0.00".  This same statement can handle any amount
from 1 cent to 99 million dollars and will keep the decimal point lined
up for amounts above and below that line.
For this thread, Ruby is much better as in Business Basic I have to find
the position of a character, replace it or remove it, and repeat.  In
Ruby this can all be done with one tr or gsub call.
This topic is locked and can not be replied to.