Skip to content
This repository
Browse code

Ruby 1.9 compat: prefer builtin String#starts_ and ends_with? if avai…

…lable [chuyeow]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8397 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 3d90733e937d11b6905ff11ce006a591ec11b4f4 1 parent 8d8b573
Jeremy Kemper authored December 15, 2007
8  activesupport/lib/active_support/core_ext/integer/even_odd.rb
@@ -10,14 +10,14 @@ module EvenOdd
10 10
         def multiple_of?(number)
11 11
           self % number == 0
12 12
         end
13  
-        
  13
+
14 14
         def even?
15 15
           multiple_of? 2
16  
-        end
17  
-        
  16
+        end if RUBY_VERSION < '1.9'
  17
+
18 18
         def odd?
19 19
           !even?
20  
-        end
  20
+        end if RUBY_VERSION < '1.9'
21 21
       end
22 22
     end
23 23
   end
6  activesupport/lib/active_support/core_ext/range/conversions.rb
... ...
@@ -1,9 +1,9 @@
1 1
 module ActiveSupport #:nodoc:
2 2
   module CoreExtensions #:nodoc:
3 3
     module Range #:nodoc:
4  
-      # Getting dates in different convenient string representations and other objects
  4
+      # Getting ranges in different convenient string representations and other objects
5 5
       module Conversions
6  
-        DATE_FORMATS = {
  6
+        RANGE_FORMATS = {
7 7
           :db => Proc.new { |start, stop| "BETWEEN '#{start.to_s(:db)}' AND '#{stop.to_s(:db)}'" }
8 8
         }
9 9
 
@@ -15,7 +15,7 @@ def self.included(base) #:nodoc:
15 15
         end
16 16
 
17 17
         def to_formatted_s(format = :default)
18  
-          DATE_FORMATS[format] ? DATE_FORMATS[format].call(first, last) : to_default_s   
  18
+          RANGE_FORMATS[format] ? RANGE_FORMATS[format].call(first, last) : to_default_s
19 19
         end
20 20
       end
21 21
     end
7  activesupport/lib/active_support/core_ext/string.rb
@@ -10,7 +10,12 @@ class String #:nodoc:
10 10
   include ActiveSupport::CoreExtensions::String::Access
11 11
   include ActiveSupport::CoreExtensions::String::Conversions
12 12
   include ActiveSupport::CoreExtensions::String::Inflections
13  
-  include ActiveSupport::CoreExtensions::String::StartsEndsWith
  13
+  if RUBY_VERSION < '1.9'
  14
+    include ActiveSupport::CoreExtensions::String::StartsEndsWith
  15
+  else
  16
+    alias starts_with? start_with?
  17
+    alias ends_with? end_with?
  18
+  end
14 19
   if defined? ActiveSupport::CoreExtensions::String::Iterators
15 20
     include ActiveSupport::CoreExtensions::String::Iterators
16 21
   end
7  activesupport/lib/active_support/core_ext/string/starts_ends_with.rb
@@ -3,6 +3,13 @@ module CoreExtensions #:nodoc:
3 3
     module String #:nodoc:
4 4
       # Additional string tests.
5 5
       module StartsEndsWith
  6
+        def self.included(base)
  7
+          base.class_eval do
  8
+            alias_method :start_with?, :starts_with?
  9
+            alias_method :end_with?, :ends_with?
  10
+          end
  11
+        end
  12
+
6 13
         # Does the string start with the specified +prefix+?
7 14
         def starts_with?(prefix)
8 15
           prefix = prefix.to_s
10  activesupport/test/core_ext/string_ext_test.rb
@@ -143,15 +143,23 @@ def test_access_returns_a_real_string
143 143
     assert_equal %w(hello), hash.keys
144 144
   end
145 145
 
146  
-  def test_starts_ends_with
  146
+  def test_starts_ends_with_alias
147 147
     s = "hello"
148 148
     assert s.starts_with?('h')
149 149
     assert s.starts_with?('hel')
150 150
     assert !s.starts_with?('el')
151 151
 
  152
+    assert s.start_with?('h')
  153
+    assert s.start_with?('hel')
  154
+    assert !s.start_with?('el')
  155
+
152 156
     assert s.ends_with?('o')
153 157
     assert s.ends_with?('lo')
154 158
     assert !s.ends_with?('el')
  159
+
  160
+    assert s.end_with?('o')
  161
+    assert s.end_with?('lo')
  162
+    assert !s.end_with?('el')
155 163
   end
156 164
 
157 165
   # FIXME: Ruby 1.9

0 notes on commit 3d90733

Please sign in to comment.
Something went wrong with that request. Please try again.