Library contents

Ruby String#ord

Get the first Unicode code point of a nonempty label.

A simple example

String#ord

  1. Start with "A"
  2. Do this Read the first code point
  3. Result 65
The Unicode code point for the letter A is decimal 65.

Syntax

Ruby syntax
value.ord
Run these examples in Ruby

Save one example at a time as lesson.rb. Use Ruby 2.6 or later.

ruby lesson.rb

Inspect an emoji feedback tag

A feedback or ticket label includes an emoji that needs a full Unicode code point. Inspect the symbol at the right boundary so the value sent to the next component represents the entire character.

From the programvalue = "😀OK"
Result128512
lesson.rb
value = "😀OK"
puts(value.ord)
Output
128512

U+1F600 has decimal value 128512.

Advertisements

Inspect a name initial

A compact printed code assigns meaning to particular positions. Locate the correct character for the requested field and check the indexing convention before sending the value to the display.

From the programvalue = "Émile"
Result201
lesson.rb
value = "Émile"
puts(value.ord)
Output
201

The precomposed capital É has decimal value 201.

Read a parcel sorting code

The depot scans a parcel marked NC-17. The scanner stores the Unicode code point of the first letter. Which decimal value should appear in the log?

GivenNC-17
Worked result78

What to watch for

An encoded byte is not necessarily a code point. These inputs contain valid Unicode and begin at a character boundary.

Documentation for String#ord