Spaces in strings

I have a string like '1 334.64' and I need to remove the space so that
it becomes '1334.64'. I have I think ruby 1.6 in home computer where
worked gsub!(/\s/, '') but in other computer I have ruby 1.7 and it
doesn't seem to work.

···

--
Posted via http://www.ruby-forum.com/.

Joonas Lindholm wrote:

Ups, what I meant is that the 1.8.6 is the version I have home and at
other computer 1.8.7. But if the gsub!(/\s/, '') looks fine I have to do
more testing..

Yep. Are you running a standalone program, or in irb? In the first case,
paste the program you are running, and its output. In the second case,
paste the irb transcript. e.g.

$ irb --simple-prompt

s = "1 334.64"

=> "1 334.64"

s.gsub!(/\s/, '')

=> "1334.64"

s

=> "1334.64"

Note that gsub! modifies the string in-place, and returns nil if no
change was made.

···

--
Posted via http://www.ruby-forum.com/\.

a = "ha ha ha ha ha ha ha"

"ha ha ha ha ha ha ha"

a.delete(" ")

"hahahahahahaha"

a

"ha ha ha ha ha ha ha"

a.delete!(" ")

"hahahahahahaha"

a

"hahahahahahaha"

···

--
Posted via http://www.ruby-forum.com/\.

Brian Candler wrote:

ruby 1.7 was only a development version, so you should move to 1.8.6 if
possible.

However, "it doesn't seem to work" is not a useful problem description.
You need to show exactly what you ran, the exact output (copy-paste),
what you expected the output to be, and what platform you are running
under.

The code snippet you have shown looks OK to me.

Ups, what I meant is that the 1.8.6 is the version I have home and at
other computer 1.8.7. But if the gsub!(/\s/, '') looks fine I have to do
more testing..

···

--
Posted via http://www.ruby-forum.com/\.

Joonas Lindholm wrote:

I have a string like '1 334.64' and I need to remove the space so that
it becomes '1334.64'. I have I think ruby 1.6 in home computer where
worked gsub!(/\s/, '') but in other computer I have ruby 1.7 and it
doesn't seem to work.

ruby 1.7 was only a development version, so you should move to 1.8.6 if
possible.

However, "it doesn't seem to work" is not a useful problem description.
You need to show exactly what you ran, the exact output (copy-paste),
what you expected the output to be, and what platform you are running
under.

The code snippet you have shown looks OK to me.

···

--
Posted via http://www.ruby-forum.com/\.

Joonas Lindholm wrote:

Brian Candler wrote:

ruby 1.7 was only a development version, so you should move to 1.8.6 if
possible.

However, "it doesn't seem to work" is not a useful problem description.
You need to show exactly what you ran, the exact output (copy-paste),
what you expected the output to be, and what platform you are running
under.

The code snippet you have shown looks OK to me.

Ups, what I meant is that the 1.8.6 is the version I have home and at
other computer 1.8.7. But if the gsub!(/\s/, '') looks fine I have to do
more testing..

I get identical results with 1.8.6 and ruby ruby 1.9.1L

str = "1 334.64"
puts str

str.gsub!(/\s/, "")
puts str

--output:--
1 334.64
1334.64

I have a hard time believing that ruby 1.8.7 would produce different
results--no matter how badly it is screwed up.

···

--
Posted via http://www.ruby-forum.com/\.

Joonas Lindholm wrote:

Brian Candler wrote:

ruby 1.7 was only a development version, so you should move to 1.8.6 if possible.

However, "it doesn't seem to work" is not a useful problem description. You need to show exactly what you ran, the exact output (copy-paste), what you expected the output to be, and what platform you are running under.

The code snippet you have shown looks OK to me.
    
Ups, what I meant is that the 1.8.6 is the version I have home and at other computer 1.8.7. But if the gsub!(/\s/, '') looks fine I have to do more testing..
  

String#tr works similar with easier interface: http://www.ruby-doc.org/core/classes/String.html#M000830

An option nobody has suggested so far: String#delete

    "1 334.64".delete(" ") # ==> "1334.64"

···

At 2009-08-26 12:37PM, "Joonas Lindholm" wrote:

I have a string like '1 334.64' and I need to remove the space so that
it becomes '1334.64'. I have I think ruby 1.6 in home computer where
worked gsub!(/\s/, '') but in other computer I have ruby 1.7 and it
doesn't seem to work.

--
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous

7stud -- wrote:

Joonas Lindholm wrote:

str = "1 334.64"
puts str

str.gsub!(/\s/, "")
puts str

--output:--
1 334.64
1334.64

The code above works fine for me on HP-UX: ruby 1.8.7 (2008-08-11
patchlevel 72) [ia64-hpux11.23].

···

--
Posted via http://www.ruby-forum.com/\.

Another option that I haven't seen suggested:

str.gsub!(/\s+/, '')

Could be a bit faster than /\s/ if there are longer sequences of spaces.

Cheers

robert

···

2009/8/26 Glenn Jackman <glennj@ncf.ca>:

At 2009-08-26 12:37PM, "Joonas Lindholm" wrote:

I have a string like '1 334.64' and I need to remove the space so that
it becomes '1334.64'. I have I think ruby 1.6 in home computer where
worked gsub!(/\s/, '') but in other computer I have ruby 1.7 and it
doesn't seem to work.

An option nobody has suggested so far: String#delete

"1 334.64".delete(" ") # ==> "1334.64"

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/