To replace multiple spaces with a single space in Ruby, you can use the `gsub` method with a regular expression pattern.
In this example,
gsub
method with the regular expression pattern /\s+/
to replace multiple spaces with a single space in the string str
.str
.str = "This is a test string"
modified_str = str.gsub(/\s+/, ' ')
puts modified_str
This is a test string
In this tutorial, we learned How to Replace multiple Spaces with a Single Space in String in Ruby language with well detailed examples.