Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby

What is the strip method in Ruby?

Hassaan Waqar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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

RELATED TAGS

ruby

CONTRIBUTOR

Hassaan Waqar
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring