Ruby on Rails, The Ultimate Guide to strip, lstip and rstrip Methods

Written on Feb 09, 2023

As a Rails developer, I'm often faced with the task of processing and manipulating strings. These useful methods for string manipulation in Ruby are strip, lstrip, and rstrip. In this blog post, we will explore the differences between these methods and how they can be used in Ruby or Rails.

What is strip in Ruby?

The strip method in Ruby is used to remove leading and trailing white spaces from a string. This is useful when you have text that is pulled in from another source and may contain unwanted white spaces at the beginning or end of the string. Here's an example of using strip:

string = "   Remove leading and trailing spaces   "
stripped_string = string.strip
puts stripped_string
# => "Remove leading and trailing spaces "

What is lstrip in Ruby?

The lstrip method in Ruby is used to remove leading white spaces from a string. This is useful when you want to remove white spaces from the beginning of a string, but keep any white spaces that appear at the end of the string. Here's an example of using lstrip:

string = "   Remove leading and trailing spaces   "
l_stripped_string = string.lstrip
puts l_stripped_string
# => "Remove leading and trailing spaces   "

What is rstrip in Ruby?

The rstrip method in Ruby is used to remove only the trailing white spaces from a string. This is useful when you have text that is pulled in from another source and may contain unwanted white spaces at the end of the string. Here's an example of using rstrip:

string = " Remove trailing spaces " 
r_stripped_string = string.rstrip 
puts r_stripped_string
# => "   Remove leading and trailing spaces"

Differences between strip, lstrip, and rstrip

Method Description
strip Removes leading and trailing white spaces
lstrip Removes leading white spaces
rstrip Removes trailing white spaces

As you can see, the main difference between strip, lstrip, and rstrip is the type of white spaces they remove. strip removes both leading and trailing white spaces, lstrip only removes leading white spaces, while rstip only removes trailing white spaces.

Performance

When it comes to performance, both strip, lstrip, and rstrip are relatively fast. To compare the performance of these methods, we can run a benchmark test using the Ruby benchmark library. Here's an example:

require 'benchmark'

string = "   When it comes to performance, both strip, lstrip and rstrip are relatively fast. To compare the performance of these methods, we can run a benchmark test using the Ruby benchmark library. Here's an example:   "
Benchmark.bm do |x|
  x.report("strip:") { 100000.times { string.strip } }
  x.report("lstrip:") { 100000.times { string.lstrip } }
  x.report("rstrip:") { 100000.times { string.rstrip } }
end
user system total real
strip 0.029657 0.060237 0.089894 ( 0.141788)
lstrip 0.035660 0.000601 0.036261 ( 0.036798)
rstrip 0.027587 0.000578 0.028165 ( 0.028700)
[#
<Benchmark::Tms:0x0000000112b2faa8 @label="strip:", @real=0.14178800000809133, @cstime=0.0, @cutime=0.0, @stime=0.060236999999999985, @utime=0.029656999999999933, @total=0.08989399999999992>, #

<Benchmark::Tms:0x0000000112b2fa08 @label="lstrip:", @real=0.036798000102862716, @cstime=0.0, @cutime=0.0, @stime=0.0006009999999999627, @utime=0.035660000000000025, @total=0.03626099999999999>, #

<Benchmark::Tms:0x0000000112b2f9b8 @label="rstrip:", @real=0.02870000002440065, @cstime=0.0, @cutime=0.0, @stime=0.0005779999999999674, @utime=0.027587000000000028, @total=0.028164999999999996>
]

The results of the benchmark test shows that strip, lstrip and rstrip are very fast methods, with rstrip being slightly faster than both strip and lstrip in this case. However, the difference in performance is very small and likely to be negligible in most real-world applications.

Combination with other methods

Each of strip, lstrip, and rstrip can be combined with other string methods in Ruby to achieve more complex string manipulations. For example, you can use gsub to replace specific characters in a string, and then use strip, lstrip, or rstrip to remove any whitespace from the start or end. Here's an example using split() method to split a string into an array based on a specific delimiter, and then using lstrip to remove any whitespace from the left of each element:

text = " apple , banana , cherry "

s_text = text.split(",").map(&:strip).to_s
puts 'Strip =' + s_text 
# => Strip =["apple", "banana", "cherry"]

l_text = text.split(",").map(&:lstrip).to_s
puts 'lStrip =' + l_text 
# => lStrip =["apple ", "banana ", "cherry "]

r_text = text.split(",").map(&:rstrip).to_s
puts 'rStrip =' + r_text 
# => rStrip =[" apple", " banana", " cherry"]

By combining strip, lstrip andrstrip` with other string methods, you can easily manipulate and clean up strings in your code.

Final thoughts

Choosing between strip, lstrip and rstrip depends on the specific use case and the desired outcome. If you need to remove whitespaces from both ends of a string, then use strip. If you only need to remove whitespaces from the left end, then use lstrip. And If you only need to remove whitespaces from the right end, then use rstrip.
It's always good to test these methods and choose the one that meets your requirements and performs best.

In conclusion, understanding the differences between strip, lstrip and rstrip is important to make the right choice and get the expected results in your Ruby on Rails code. By following the advice and examples in this article, you'll be able to make an informed decision and use the right method for the job.

And as usual, Happy Coding 😀 💻

Subscribe to Design and Develpment ideas

Enter your email to subscribe to a once-monthly newsletter curating the latest content on Rails, Hotwire, and other things you might find interesting.

Get in touch