rtrim

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

    PHP

    $result = rtrim("hello world \n");
    var_export($result);
    // => 'hello world'

    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.

    PHP

    $result = rtrim("/Users/joe/work/ ", " /");
    var_export($result);
    // => '/Users/joe/work'

    Ruby

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

    see also

rmdir sha1

Looking for Rails or PHP web application development, integration, and training?

Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.