What is the strip method in Ruby?

The strip method in Ruby creates a copy of the original string with the leading and trailing whitespace removed. The following characters constitute whitespace elements:

Character Symbol
null \x00
horizontal tab \t
vertical tab \v
new line \n
form feed \f
carriage return \r
space " "

The illustration below shows how strip works in Ruby:

How strip method works in Ruby

Return value

The strip method returns a copy of the original string with trailing and leading whitespace removed.

Example

The code snippet below shows how we can use strip in Ruby:

puts "\vstring\t\n ".strip
puts " string ".strip

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved