Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

String#squeeze

(*::String::selector selectors) -> ::String
squeeze(*selectors) → new_string

Returns a copy of self with characters specified by selectors “squeezed” (see Multiple Character Selectors):

“Squeezed” means that each multiple-character run of a selected character is squeezed down to a single character; with no arguments given, squeezes all characters:

Example
"yellow moon".squeeze                  #=> "yelow mon"
"  now   is  the".squeeze(" ")         #=> " now is the"
"putters shoot balls".squeeze("m-z")   #=> "puters shot balls"

Ruby version changes

  1. 3.0
See also #squeeze! #split #slice! #start_with? also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0