Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Latest commit

 

History

History
36 lines (28 loc) · 692 Bytes

rtrim.markdown

File metadata and controls

36 lines (28 loc) · 692 Bytes

rtrim

Stripping whitespace from the end of a string is done in Ruby using the String#rstrip method.

{{code:php $result = rtrim("hello world \n"); var_export($result); // => 'hello world' }}

{{code:ruby p "hello world \n".rstrip # => "hello world" }}

PHP's rtrim function has a second argument to specify the characters to strip. We can do this in Ruby using the String#gsub method.

{{code:php $result = rtrim("/Users/joe/work/ ", " /"); var_export($result); // => '/Users/joe/work' }}

{{code:ruby chars = " /" p "/Users/joe/work/ ".gsub(/[#{chars}]+$/, '') # => "/Users/joe/work" }}

{{related: strings/trim strings/ltrim }}