instance method ord

Ruby on Rails 3.1.12

Since v3.0.20 Last seen in v3.2.22.5

Available in: v3.0.20 v3.1.12 v3.2.22.5

Signature

ord()

Returns the codepoint of the first character of the string, assuming a single-byte character encoding:

"a".ord # => 97
"à".ord # => 224, in ISO-8859-1

This method is defined in Ruby 1.8 for Ruby 1.9 forward compatibility on these character encodings.

ActiveSupport::Multibyte::Chars#ord is forward compatible with Ruby 1.9 on UTF8 strings:

"a".mb_chars.ord # => 97
"à".mb_chars.ord # => 224, in UTF8

Note that the 224 is different in both examples. In ISO-8859-1 “à” is represented as a single byte, 224. In UTF8 it is represented with two bytes, namely 195 and 160, but its Unicode codepoint is 224. If we call ord on the UTF8 string “à” the return value will be 195. That is not an error, because UTF8 is unsupported, the call itself would be bogus.

Source
# File activesupport/lib/active_support/core_ext/string/conversions.rb, line 28
  def ord
    self[0]
  end

Defined in activesupport/lib/active_support/core_ext/string/conversions.rb line 28 · View on GitHub · Improve this page · Find usages on GitHub

Defined in String

Type at least 2 characters to search.

↑↓ navigate · open · esc close