Skip to content

Commit

Permalink
Add ActiveSupport::Multibyte::Chars#ord method so that it returns cor…
Browse files Browse the repository at this point in the history
…rect Unicode value instead of falling back on String#ord in CoreExtensions, which is not multibyte compatible

[#1483 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
Jason Cheow authored and jeremy committed Dec 9, 2008
1 parent fc037be commit 0329021
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion activesupport/CHANGELOG
@@ -1,4 +1,9 @@
*2.2 (November 21st, 2008)*
*2.2.3 (next release)*

* Multibyte: add multibyte-safe Chars#ord rather than falling back to String#ord. #1483 [Jason Cheow]


*2.2.2 (November 21st, 2008)*

* TimeZone offset tests: use current_period, to ensure TimeZone#utc_offset is up-to-date [Geoff Buesing]

Expand Down
8 changes: 8 additions & 0 deletions activesupport/lib/active_support/multibyte/chars.rb
Expand Up @@ -344,6 +344,14 @@ def slice(*args)
end
alias_method :[], :slice

# Converts first character in the string to Unicode value
#
# Example:
# 'こんにちは'.mb_chars.ord #=> 12371
def ord
self.class.u_unpack(@wrapped_string)[0]
end

# Convert characters in the string to uppercase.
#
# Example:
Expand Down
4 changes: 4 additions & 0 deletions activesupport/test/multibyte_chars_test.rb
Expand Up @@ -397,6 +397,10 @@ def test_slice_should_throw_exceptions_on_invalid_arguments
assert_raise(ArgumentError) { @chars.slice(1, 1, 1) }
end

def test_ord_should_return_unicode_value_for_first_character
assert_equal 12371, @chars.ord
end

def test_upcase_should_upcase_ascii_characters
assert_equal '', ''.mb_chars.upcase
assert_equal 'ABC', 'aBc'.mb_chars.upcase
Expand Down

0 comments on commit 0329021

Please sign in to comment.