From 96924d9f2f55be77849405d748269d6f9cd05a4f Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Sat, 14 Feb 2009 15:49:10 +0900 Subject: [PATCH 1/2] Ruby 1.9 compat: add String#bytesize for Ruby 1.9 compatibility --- .../lib/active_support/core_ext/string.rb | 2 ++ .../lib/active_support/core_ext/string/size.rb | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 0 deletions(-) create mode 100644 activesupport/lib/active_support/core_ext/string/size.rb diff --git a/activesupport/lib/active_support/core_ext/string.rb b/activesupport/lib/active_support/core_ext/string.rb index 16c544a..74c61b4 100644 --- a/activesupport/lib/active_support/core_ext/string.rb +++ b/activesupport/lib/active_support/core_ext/string.rb @@ -9,6 +9,7 @@ require 'active_support/core_ext/string/multibyte' require 'active_support/core_ext/string/xchar' require 'active_support/core_ext/string/filters' require 'active_support/core_ext/string/behavior' +require 'active_support/core_ext/string/size' class String #:nodoc: include ActiveSupport::CoreExtensions::String::Access @@ -19,4 +20,5 @@ class String #:nodoc: include ActiveSupport::CoreExtensions::String::Iterators include ActiveSupport::CoreExtensions::String::Behavior include ActiveSupport::CoreExtensions::String::Multibyte + include ActiveSupport::CoreExtensions::String::Size end diff --git a/activesupport/lib/active_support/core_ext/string/size.rb b/activesupport/lib/active_support/core_ext/string/size.rb new file mode 100644 index 0000000..010e0d3 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/string/size.rb @@ -0,0 +1,15 @@ +module ActiveSupport #:nodoc: + module CoreExtensions #:nodoc: + module String #:nodoc: + module Size + def self.append_features(base) + unless '1.8.7 and later'.respond_to?(:bytesize) + base.class_eval do + alias_method :bytesize, :size + end + end + end + end + end + end +end -- 1.6.1.2 From 02b26993312ed696a6fb72165568f330597edab0 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Sat, 14 Feb 2009 18:44:43 +0900 Subject: [PATCH 2/2] Ruby 1.9 compat: add a test for String#bytesize --- activesupport/test/core_ext/string_ext_test.rb | 14 ++++++++++++++ 1 files changed, 14 insertions(+), 0 deletions(-) diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index e232bf8..726a577 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -254,3 +254,17 @@ class CoreExtStringMultibyteTest < ActiveSupport::TestCase end end end + +class CoreExtStringBytesizeTest < ActiveSupport::TestCase + UNICODE_STRING = 'おはよう' + + if RUBY_VERSION < '1.8.7' + def test_core_ext_adds_bytesize + assert UNICODE_STRING.respond_to?(:bytesize) + end + + def test_bytesize_returns_size + assert UNICODE_STRING.bytesize == UNICODE_STRING.size + end + end +end -- 1.6.1.2