Logo

What is the Ruby function to remove all white spaces?

Removing all whitespace from a string is a common task in many programming languages, including Ruby. While Ruby does not offer a single built-in method specifically named “remove all whitespace,” there are two popular techniques to achieve this: using a regular expression with gsub or using the delete method. Below, we’ll explore both methods and discuss which is most suitable, depending on the type of whitespace you want to remove.

1. Using gsub with a Regular Expression

The most flexible way to remove all whitespace (including spaces, tabs, newlines, etc.) is by using the String#gsub method with the \s (whitespace) character class in a regular expression:

string = " Hello \t World \n " no_whitespace = string.gsub(/\s+/, "") puts no_whitespace # => "HelloWorld"
  • \s matches any whitespace character (spaces, tabs, newlines, carriage returns, form feeds).
  • \s+ matches one or more consecutive whitespace characters.
  • "" (the replacement string) ensures all matching characters are removed.

Why Use gsub?

  • Handles All Whitespace: If your string may contain tabs, newlines, or any other whitespace, a regex is the safest bet.
  • Fine-Grained Control: You can modify the pattern to suit your needs (e.g., remove only tabs or only certain whitespace types).

2. Using delete for Simpler Cases

If your string only contains regular space characters (no tabs or newlines) and you just want to remove those, you can use String#delete:

string = " Hello World " no_spaces = string.delete(" ") puts no_spaces # => "HelloWorld"

However, delete(" ") will only remove the ASCII space character (" "). It won’t remove tabs ("\t") or newlines ("\n"). You can specify additional whitespace characters:

string = "Hello \t World \n" no_spaces = string.delete(" \t\n") puts no_spaces # => "HelloWorld"

When to Use delete

  • Known Whitespace: If you’re certain about which whitespace characters appear (e.g., only spaces and tabs).
  • Slightly Faster: In some scenarios, delete might be marginally faster than using a regex, though this difference is usually negligible.

Common Pitfalls

  1. Trailing vs. All Whitespace
    Ruby’s strip or lstrip and rstrip methods only remove whitespace from the ends (leading or trailing). If you need to remove all whitespace everywhere, you must use gsub or delete.

  2. Unicode Whitespace
    If you’re dealing with Unicode strings that contain non-ASCII whitespace characters (e.g., non-breaking spaces, zero-width spaces), you need to ensure your regex handles those. In that case, you might need something like /[[:space:]]+/ instead of /\s+/.

  3. Performance
    For most practical use cases, the performance difference between gsub and delete is negligible. Optimize only if you’re dealing with extremely large datasets in tight loops.

Boost Your Ruby Skills Further

Removing whitespace is just one of countless string manipulation tasks you’ll encounter in Ruby. If you’re preparing for interviews or looking to deepen your coding and system design expertise, check out these DesignGurus.io courses:

If you want personalized feedback, you can also schedule a Coding Mock Interview or a System Design Mock Interview with ex-FAANG engineers.

Conclusion

To remove all whitespace from a string in Ruby, you can either use:

  1. gsub(/\s+/, "") for a regex-based solution that handles all forms of whitespace (spaces, tabs, newlines, etc.).
  2. delete(" ") (optionally specifying other whitespace characters like \t, \n) if you only want to remove certain types of whitespace.

Both approaches are simple and effective, so the choice often comes down to the specific whitespace characters involved and your performance requirements. By mastering these techniques and continuing to develop your coding skills, you’ll be well-prepared to tackle string manipulation tasks in any Ruby application.

CONTRIBUTOR
TechGrind