Ruby Method of the Day - String.rstrip!

Posted by Kelly McCauley on Sep 26, 2007

Signature

string.rstrip!    #=> string or nil

String.rstrip! is the in-place edit version of String.rstrip. string.rstrip! returns string with trailing whitespace removed from string or it returns nil if no trailing whitespace was found.

Examples

1
2
3
4
5
6
str = "foo bar \r\n\t"    #=> "foo bar \r\n\t"
str.rstrip!               #=> "foo bar"
str == "foo bar \r\n\t"   #=> false
str == "foo bar"          #=> true
str.rstrip!               #=> nil

Documentation Reference

www.ruby-doc.org : String.rstrip!