M
Michael W. Ryder
I was trying to come up with a way to remove x instances of a character
from a string and came up with a problem. If I enter:
a = "a b c d e f"
for i in 1..3
a = a.sub!(' ', '')
end
puts a ==> returns 'abcd e f' which is correct.
But if I enter:
a = "a b c d e f"
for i in 1..10
a = a.sub!(' ', '')
end
puts a ==> returns error.rb:3: private method `sub!' called for
nil:NilClass (NoMethodError, and a is now nil.
What I am looking for is a way to remove the first n instances of a
blank from the string without wiping out the string if it does not
contain at least n blanks. I assume there is a way to do this with
regular expressions, but have not found it yet. This is something an
editor I liked, UCEDIT, on the CDC Cyber had in the 70's.
from a string and came up with a problem. If I enter:
a = "a b c d e f"
for i in 1..3
a = a.sub!(' ', '')
end
puts a ==> returns 'abcd e f' which is correct.
But if I enter:
a = "a b c d e f"
for i in 1..10
a = a.sub!(' ', '')
end
puts a ==> returns error.rb:3: private method `sub!' called for
nil:NilClass (NoMethodError, and a is now nil.
What I am looking for is a way to remove the first n instances of a
blank from the string without wiping out the string if it does not
contain at least n blanks. I assume there is a way to do this with
regular expressions, but have not found it yet. This is something an
editor I liked, UCEDIT, on the CDC Cyber had in the 70's.