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
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved