What is string ord in Ruby?
Overview
The ord method is used to return the integer ordinal of a one-character string.
Syntax
str.ord
Parameters
str: This is the one-character string that we want to get the integer ordinal of.
Return value
An integer value is returned which is the ordinal of a one-character string.
Example
# create one-character stringsstr1 = "a"str2 = "b"str3 = "c"str4 = "z"# get integer ordinala = str1.ordb = str2.ordc = str3.ordd = str4.ord# print resultsputs aputs bputs cputs d
Explanation
-
Line 2–5: We create one-character strings.
-
Line 8–11: We call the
ordmethod on the one-character strings and save the results on variablesa,b,c, andd. -
Line 14–17: We print the results, which are the integer ordinals of the string on the console.