Question regarding tr method in Strings Class

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.

On 5/3/07, Michael W. Ryder [email protected] 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 :wink:

Cheers
Robert

From: Michael W. Ryder [mailto:[email protected]]

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”

Robert D. 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 :wink:

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.

On 5/3/07, Michael W. Ryder [email protected] wrote:

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

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.

On 5/3/07, Michael W. Ryder [email protected] 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/

On 5/3/07, Brian C. [email protected] 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)

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

On Fri, May 04, 2007 at 10:47:15PM +0900, Logan C. wrote:

Tweak:
REPLACE_RE = Regexp.union(*REPLACE.keys)

Ooh, shiny.

Rick DeNatale wrote:

On 5/3/07, Michael W. Ryder [email protected] 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.