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 strings
str1 = "a"
str2 = "b"
str3 = "c"
str4 = "z"
# get integer ordinal
a = str1.ord
b = str2.ord
c = str3.ord
d = str4.ord
# print results
puts a
puts b
puts c
puts d

Explanation

  • Line 2–5: We create one-character strings.

  • Line 8–11: We call the ord method on the one-character strings and save the results on variables a, b, c, and d.

  • Line 14–17: We print the results, which are the integer ordinals of the string on the console.