Permalink
Browse files

Ruby 1.9 compat: 'a'.ord == 'a'[0]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8403 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent aa4ad40 commit 6d5ee8dab37f7cb4a15ab7a3c4369b105055570e @jeremy jeremy committed Dec 15, 2007
View
9 activesupport/lib/active_support/core_ext/string/conversions.rb
@@ -5,6 +5,11 @@ module CoreExtensions #:nodoc:
module String #:nodoc:
# Converting strings to other objects
module Conversions
+ # 'a'.ord == 'a'[0] for Ruby 1.9 forward compatibility.
+ def ord
+ self[0]
+ end if RUBY_VERSION < '1.9'
+
# Form can be either :utc (default) or :local.
def to_time(form = :utc)
::Time.send("#{form}_time", *ParseDate.parsedate(self)[0..5].map {|arg| arg || 0})
@@ -13,11 +18,11 @@ def to_time(form = :utc)
def to_date
::Date.new(*ParseDate.parsedate(self)[0..2])
end
-
+
def to_datetime
::DateTime.civil(*ParseDate.parsedate(self)[0..5].map {|arg| arg || 0} << 0)
end
end
end
end
-end
+end
View
5 activesupport/test/core_ext/string_ext_test.rb
@@ -78,6 +78,11 @@ def test_humanize
end
end
+ def test_ord
+ assert_equal 97, 'a'.ord
+ assert_equal 97, 'abc'.ord
+ end
+
def test_string_to_time
assert_equal Time.utc(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time
assert_equal Time.local(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time(:local)

0 comments on commit 6d5ee8d

Please sign in to comment.