ltrim

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

    PHP

    $result = ltrim(" \nhello world");
    var_export($result);
    // => 'hello world'

    Ruby

    p " \nhello world".lstrip
    # => "hello world"

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

    PHP

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

    Ruby

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

    see also

lstat md5

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.