How to remove the first character of a string in Ruby using chr

Overview

The chr method removes the first character of a string in Ruby. It removes the one-character string at the beginning of a string and returns this character.

Syntax

str.chr

Parameters

str: This is the string that calls the chr method.

Return value

The string’s first character is returned, which calls the chr method.

# create some strings
str1 = "Welcome"
str2 = "to"
str3 = "Edpresso"
# remove first characters
a = str1.chr
b = str2.chr
c = str3.chr
# print returned values
puts a
puts b
puts c

As we can see above, the first characters of the strings were removed and printed out on the console.

Free Resources