From 3d90733e937d11b6905ff11ce006a591ec11b4f4 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 15 Dec 2007 02:27:11 +0000 Subject: Ruby 1.9 compat: prefer builtin String#starts_ and ends_with? if available [chuyeow] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8397 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/lib/active_support/core_ext/integer/even_odd.rb | 8 ++++---- activesupport/lib/active_support/core_ext/range/conversions.rb | 6 +++--- activesupport/lib/active_support/core_ext/string.rb | 7 ++++++- .../lib/active_support/core_ext/string/starts_ends_with.rb | 7 +++++++ activesupport/test/core_ext/string_ext_test.rb | 10 +++++++++- 5 files changed, 29 insertions(+), 9 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/core_ext/integer/even_odd.rb b/activesupport/lib/active_support/core_ext/integer/even_odd.rb index 3762308cc3..7ea4cbf2ba 100644 --- a/activesupport/lib/active_support/core_ext/integer/even_odd.rb +++ b/activesupport/lib/active_support/core_ext/integer/even_odd.rb @@ -10,14 +10,14 @@ module ActiveSupport #:nodoc: def multiple_of?(number) self % number == 0 end - + def even? multiple_of? 2 - end - + end if RUBY_VERSION < '1.9' + def odd? !even? - end + end if RUBY_VERSION < '1.9' end end end diff --git a/activesupport/lib/active_support/core_ext/range/conversions.rb b/activesupport/lib/active_support/core_ext/range/conversions.rb index 77e86a6ae6..3d12605f4d 100644 --- a/activesupport/lib/active_support/core_ext/range/conversions.rb +++ b/activesupport/lib/active_support/core_ext/range/conversions.rb @@ -1,9 +1,9 @@ module ActiveSupport #:nodoc: module CoreExtensions #:nodoc: module Range #:nodoc: - # Getting dates in different convenient string representations and other objects + # Getting ranges in different convenient string representations and other objects module Conversions - DATE_FORMATS = { + RANGE_FORMATS = { :db => Proc.new { |start, stop| "BETWEEN '#{start.to_s(:db)}' AND '#{stop.to_s(:db)}'" } } @@ -15,7 +15,7 @@ module ActiveSupport #:nodoc: end def to_formatted_s(format = :default) - DATE_FORMATS[format] ? DATE_FORMATS[format].call(first, last) : to_default_s + RANGE_FORMATS[format] ? RANGE_FORMATS[format].call(first, last) : to_default_s end end end diff --git a/activesupport/lib/active_support/core_ext/string.rb b/activesupport/lib/active_support/core_ext/string.rb index b3e1b1189a..5497b6f6f6 100644 --- a/activesupport/lib/active_support/core_ext/string.rb +++ b/activesupport/lib/active_support/core_ext/string.rb @@ -10,7 +10,12 @@ class String #:nodoc: include ActiveSupport::CoreExtensions::String::Access include ActiveSupport::CoreExtensions::String::Conversions include ActiveSupport::CoreExtensions::String::Inflections - include ActiveSupport::CoreExtensions::String::StartsEndsWith + if RUBY_VERSION < '1.9' + include ActiveSupport::CoreExtensions::String::StartsEndsWith + else + alias starts_with? start_with? + alias ends_with? end_with? + end if defined? ActiveSupport::CoreExtensions::String::Iterators include ActiveSupport::CoreExtensions::String::Iterators end diff --git a/activesupport/lib/active_support/core_ext/string/starts_ends_with.rb b/activesupport/lib/active_support/core_ext/string/starts_ends_with.rb index 67174019db..3960669798 100644 --- a/activesupport/lib/active_support/core_ext/string/starts_ends_with.rb +++ b/activesupport/lib/active_support/core_ext/string/starts_ends_with.rb @@ -3,6 +3,13 @@ module ActiveSupport #:nodoc: module String #:nodoc: # Additional string tests. module StartsEndsWith + def self.included(base) + base.class_eval do + alias_method :start_with?, :starts_with? + alias_method :end_with?, :ends_with? + end + end + # Does the string start with the specified +prefix+? def starts_with?(prefix) prefix = prefix.to_s diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index 722bb24a37..cec6772272 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -143,15 +143,23 @@ class StringInflectionsTest < Test::Unit::TestCase assert_equal %w(hello), hash.keys end - def test_starts_ends_with + def test_starts_ends_with_alias s = "hello" assert s.starts_with?('h') assert s.starts_with?('hel') assert !s.starts_with?('el') + assert s.start_with?('h') + assert s.start_with?('hel') + assert !s.start_with?('el') + assert s.ends_with?('o') assert s.ends_with?('lo') assert !s.ends_with?('el') + + assert s.end_with?('o') + assert s.end_with?('lo') + assert !s.end_with?('el') end # FIXME: Ruby 1.9 -- cgit v1.2.3