Newbie question - how to replace multiple whitespace within a string?

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

Brian Tully wrote:

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?! :slight_smile:

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.

Brian Tully wrote:

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"

Simply MAH-velous! :slight_smile: 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

···

on 6/22/04 3:01 PM, Jamey Cribbs at cribbsj@oakwood.org wrote:

Brian Tully wrote:

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?! :slight_smile:

Brian Tully wrote:

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

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?

···

on 6/22/04 3:36 PM, Joel VanderWerf at vjoel@PATH.Berkeley.EDU wrote:

Brian Tully wrote:

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

Brian Tully wrote:

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.

Brian Tully wrote:

Brian Tully wrote:

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

···

on 6/22/04 3:36 PM, Joel VanderWerf at vjoel@PATH.Berkeley.EDU wrote:

--
Mark Sparshatt

Brian Tully wrote:

···

on 6/22/04 3:36 PM, Joel VanderWerf at vjoel@PATH.Berkeley.EDU wrote:

Brian Tully wrote:

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

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.

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 :slight_smile:

That clears it up for me :slight_smile:

Best regards,
brian