Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
communitycreator
whitespace

What is lstrip in Ruby?

okwudili onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

lstrip in Ruby is used to remove the leading whitespace characters in a string.

A whitespace character could be a space, null, horizontal tab, line feed, vertical tab, form feed, or carriage return.

Syntax

str.lstrip

where str is the string whose leading whitespace characters we want to remove.

Parameters

This function doesn’t take any parameters.

Return value

It returns the string str with any leading whitespace characters removed.

Code example

# create strings
str1 = "    Edpresso"
str2 = "is"
str3 = "\n\t\rThe"
str4 = "\Best     !"

# remove leading 
# whitespace characters
a = str1.lstrip
b = str2.lstrip
c = str3.lstrip
d = str4.lstrip

# print results
puts a
puts b
puts c
puts d
Remove the leading whitespace characters from a string

Explanation

  • Lines 2-5: We create some strings from which we want to remove the leading whitespace characters.

  • Lines 9-12: We use the lstrip method to remove the leading whitespace characters from the strings and store the returned values in variables a, b, c, and d.

  • Lines 15-18: We print the results to the console.

RELATED TAGS

ruby
communitycreator
whitespace

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring