Newbie question - how to replace multiple whitespace within astring?

B

Brian Tully

Sorry if this is too basic a question but I just inherited a handful of Ruby
code and I'm looking to do something that is unbelievably simple to do with
PHP or Perl, but I'm having no luck figuring out how to do it in Ruby (no
thanks to "Ruby In A Nutshell").

In the middle of a Ruby script I have a loop with the variable:

@user['state']

Which is populated by data from an external text file. However I'm noticing
that the text file is not consistent in how it reports the user's State and
will occasionally add an extra whitespace between States comprised of 2
words, e.g., "New York".

In PHP I would simply perform an eregi_replace(" ", " ", $state); and in
Perl I would do something like $state =~ s/\s\s/\s/$state;

How would one accomplish this in Ruby?

Thanks in advance!

Regards,
Brian
 
A

Andreas Schwarz

Brian said:
In PHP I would simply perform an eregi_replace(" ", " ", $state); and in
Perl I would do something like $state =~ s/\s\s/\s/$state;

How would one accomplish this in Ruby?

"New York".gsub(/ /, ' ')
=> "New York"

- or better -

"New \t York".gsub(/\s+/, ' ')
=> "New York"
 
J

Jamey Cribbs

Brian said:
In PHP I would simply perform an eregi_replace(" ", " ", $state); and in
Perl I would do something like $state =~ s/\s\s/\s/$state;

How would one accomplish this in Ruby?
state.squeeze!(" ")

Now, how easy is that?! :)

Jamey

Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.
 
B

Brian Tully

state.squeeze!(" ")

Now, how easy is that?! :)

Simply MAH-velous! :) thanks a bunch!

Just to clarify...

Should I use state.squeeze(" ") to ensure that it always returns the state
even if nothing was changed? squeeze! seems to return nil if nothing needs
to be changed.

Thanks again!
Brian
 
J

Joel VanderWerf

Brian said:
Should I use state.squeeze(" ") to ensure that it always returns the state
even if nothing was changed? squeeze! seems to return nil if nothing needs
to be changed.

str.squeeze! is destructive (changes the String object referred to by
str), and str.squeeze is not. So it just depends on whether you need to
keep the original string intact (I'd guess not).
 
B

Brian Tully

str.squeeze! is destructive (changes the String object referred to by
str), and str.squeeze is not. So it just depends on whether you need to
keep the original string intact (I'd guess not).


Hmmm I'm a little hazy...

I want Ruby to strip out any instances of multiple whitespace and return the
"correct" string. I also want it to return the string regardless of whether
the squeeze function modified it.

So in these examples I would hope for the following:

@state = @user['state'].squeeze(" ")

if @user['state'] was originally "New York" I would hope that the above
would set @state to "New York".

In addition if @user['state'] was originally "California" I would hope that
the above would still set @state to "California".



squeeze! - Squeezes str in place, returning either str, or nil if no
changes were made.

I interpret this as if no changes were made "nil" will be returned, so in my
example of "California" wouldn't this set @state to nil?
 
J

Jamey Cribbs

Brian said:
So in these examples I would hope for the following:

@state = @user['state'].squeeze(" ")

if @user['state'] was originally "New York" I would hope that the above
would set @state to "New York".

In addition if @user['state'] was originally "California" I would hope that
the above would still set @state to "California".



squeeze! - Squeezes str in place, returning either str, or nil if no
changes were made.

I interpret this as if no changes were made "nil" will be returned, so in my
example of "California" wouldn't this set @state to nil?
You are correct.

Jamey

Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.
 
M

Mark Sparshatt

Brian said:
Brian Tully wrote:



str.squeeze! is destructive (changes the String object referred to by
str), and str.squeeze is not. So it just depends on whether you need to
keep the original string intact (I'd guess not).


Hmmm I'm a little hazy...

I want Ruby to strip out any instances of multiple whitespace and return the
"correct" string. I also want it to return the string regardless of whether
the squeeze function modified it.

So in these examples I would hope for the following:

@state = @user['state'].squeeze(" ")

if @user['state'] was originally "New York" I would hope that the above
would set @state to "New York".

In addition if @user['state'] was originally "California" I would hope that
the above would still set @state to "California".
this is correct

@user["state"] = "New york"
@state = @user["state"].squeeze
p @state #=> "New york"

@user["state"] = "California"
@state = @user["state"].squeeze
p @state #=> "California"
squeeze! - Squeezes str in place, returning either str, or nil if no
changes were made.

I interpret this as if no changes were made "nil" will be returned, so in my
example of "California" wouldn't this set @state to nil?
correct

you would use squeeze if you wanted to modify @user["state"]. In which
case the following lines are about equivilent

@user["state"] = @user["state"].squeeze
@user["state"].squeeze!
 
B

Brian Tully

squeeze! - Squeezes str in place, returning either str, or nil if no
changes were made.

I interpret this as if no changes were made "nil" will be returned, so in my
example of "California" wouldn't this set @state to nil?
correct

you would use squeeze if you wanted to modify @user["state"]. In which
case the following lines are about equivilent

@user["state"] = @user["state"].squeeze
@user["state"].squeeze!

Aha! Thanks Mark :)

That clears it up for me :)

Best regards,
brian
 
J

Joel VanderWerf

Brian said:
Brian Tully wrote:



str.squeeze! is destructive (changes the String object referred to by
str), and str.squeeze is not. So it just depends on whether you need to
keep the original string intact (I'd guess not).



Hmmm I'm a little hazy...

I want Ruby to strip out any instances of multiple whitespace and return the
"correct" string. I also want it to return the string regardless of whether
the squeeze function modified it.

So in these examples I would hope for the following:

@state = @user['state'].squeeze(" ")

if @user['state'] was originally "New York" I would hope that the above
would set @state to "New York".

In addition if @user['state'] was originally "California" I would hope that
the above would still set @state to "California".

This is one way:

@state = @user['state']
@state.squeeze!(" ")

After this code, the string referenced by both @state and @user['state']
will be have no successive pairs of whitespace.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top