To remove any leading white spaces (tabs, spaces, and new lines) from a string in Ruby, we can use a regular expression with the sub
method. The regular expression matches any leading white spaces and replaces them with an empty string. Here is an example:
main.rb43 chars3 lines
In this example, the ^\s+
regular expression matches one or more leading white spaces at the beginning of the string. The sub
method replaces the matched substring with an empty string.
After running this code, the value of str
will be "hello world"
.
gistlibby LogSnag