Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Extract String#bytesize shim

  • Loading branch information...
commit a606727606cc0725a39748dd9d310b2b064e3ca7 1 parent cdf60e4
Jeremy Kemper jeremy authored
5 activesupport/lib/active_support/core_ext/string/bytesize.rb
View
@@ -0,0 +1,5 @@
+unless '1.9'.respond_to?(:bytesize)
+ class String
+ alias :bytesize :size
+ end
+end
5 activesupport/lib/active_support/core_ext/string/interpolation.rb
View
@@ -6,6 +6,7 @@
=end
if RUBY_VERSION < '1.9'
+ require 'active_support/core_ext/string/bytesize'
# KeyError is raised by String#% when the string contains a named placeholder
# that is not contained in the given arguments hash. Ruby 1.9 includes and
@@ -24,8 +25,6 @@ def initialize(message = nil)
# the meaning of the msgids using "named argument" instead of %s/%d style.
class String
- # For older ruby versions, such as ruby-1.8.5
- alias :bytesize :size unless instance_methods.find {|m| m.to_s == 'bytesize'}
alias :interpolate_without_ruby_19_syntax :% # :nodoc:
INTERPOLATION_PATTERN = Regexp.union(
@@ -90,4 +89,4 @@ def %(args)
end
end
end
-end
+end
7 activesupport/test/core_ext/string_ext_test.rb
View
@@ -345,3 +345,10 @@ def test_string_interpolation_raises_an_argument_error_when_mixing_named_and_unn
assert_raises(ArgumentError) { "%{name} %f" % [1.0, 2.0] }
end
end
+
+class StringBytesizeTest < Test::Unit::TestCase
+ def test_bytesize
+ assert_respond_to 'foo', :bytesize
+ assert_equal 3, 'foo'.bytesize
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.