Ruby - Regular ExpressionsHow can you use Ruby regex to replace all sequences of whitespace with a single space in a string?Atext.scan(/\s+/, ' ')Btext.match(/\s+/, ' ')Ctext.gsub(/\s+/, ' ')Dtext.split(/\s+/, ' ')Check Answer
Step-by-Step SolutionSolution:Step 1: Identify regex for whitespace sequencesThe pattern \s+ matches one or more whitespace characters.Step 2: Use gsub to replace matchesgsub replaces all occurrences of the pattern with the given string, here a single space.Final Answer:text.gsub(/\s+/, ' ') -> Option CQuick Check:Use gsub with regex to replace patterns [OK]Quick Trick: Use gsub(/\s+/, ' ') to normalize whitespace [OK]Common Mistakes:Using scan or match for replacementUsing split incorrectly with replacementNot using + quantifier in regex
Master "Regular Expressions" in Ruby9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Ruby Quizzes Advanced Metaprogramming - Inherited hook - Quiz 7medium Concurrent Programming - GIL (Global Interpreter Lock) impact - Quiz 2easy Concurrent Programming - Thread safety concepts - Quiz 10hard Functional Patterns in Ruby - Proc composition - Quiz 5medium Gems and Bundler - Bundler for dependency resolution - Quiz 9hard Gems and Bundler - Bundler for dependency resolution - Quiz 14medium Ruby Ecosystem and Best Practices - Ruby style guide essentials - Quiz 12easy Ruby Ecosystem and Best Practices - Debugging with pry and byebug - Quiz 7medium Testing with RSpec and Minitest - RSpec describe and it blocks - Quiz 4medium Testing with RSpec and Minitest - Let and before hooks - Quiz 11easy