Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ordinal
ruby
method
communitycreator

What is string ord 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

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
An example of using the ord method

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.

RELATED TAGS

ordinal
ruby
method
communitycreator

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