Ruby removal method (string management)

I am new to Ruby and have worked my way through Mr. Neighborly Humble Little Ruby Guide. There were several typos in the code examples along the way, but I always managed to figure out what was wrong, and then fix it - so far!

It is really simple, but I can not get the following example for working with Mac OS X (Snow Leopard):

gone = "Got gone fool!"
puts "Original: " + gone
gone.delete!("o", "r-v")
puts "deleted: " + gone

The output I expect is:

Original: Got gone fool!
deleted: G gne fl!

The output I really get is:

Original: Got gone fool!
deleted: Got gone fool!

Delete! the method did not seem to have any effect.

Can anyone shed light on what is happening here ?: - \

+5
source share
3 answers

String.delete (Documented here) , .

2 - , . , gone.delete!("o", "r-v")

gone.delete ['o'] & ['r','s','t','u','v']

, , .

+9

gone.delete!("o", "r-v")

to

gone.delete!("or-v")

.

+2

You get the same o / p using a different method like gsub

puts "deleted: " + gone.gsub('o', '')

o / r

deleted: Got gone fool!
+1
source

All Articles