Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert integer extension modules to class reopens
- Loading branch information
Showing
3 changed files
with
27 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
require 'active_support/core_ext/integer/even_odd' | ||
require 'active_support/core_ext/integer/inflections' | ||
|
||
require 'active_support/core_ext/util' | ||
ActiveSupport.core_ext Integer, %w(even_odd inflections time) | ||
ActiveSupport.core_ext Integer, %w(time) |
39 changes: 13 additions & 26 deletions
39
activesupport/lib/active_support/core_ext/integer/even_odd.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,16 @@ | ||
module ActiveSupport #:nodoc: | ||
module CoreExtensions #:nodoc: | ||
module Integer #:nodoc: | ||
# For checking if a fixnum is even or odd. | ||
# | ||
# 2.even? # => true | ||
# 2.odd? # => false | ||
# 1.even? # => false | ||
# 1.odd? # => true | ||
# 0.even? # => true | ||
# 0.odd? # => false | ||
# -1.even? # => false | ||
# -1.odd? # => true | ||
module EvenOdd | ||
def multiple_of?(number) | ||
self % number == 0 | ||
end | ||
class Integer | ||
# Check whether the integer is evenly divisible by the argument. | ||
def multiple_of?(number) | ||
self % number == 0 | ||
end | ||
|
||
def even? | ||
multiple_of? 2 | ||
end if RUBY_VERSION < '1.9' | ||
# Is the integer a multiple of 2? | ||
def even? | ||
multiple_of? 2 | ||
end if RUBY_VERSION < '1.9' | ||
|
||
def odd? | ||
!even? | ||
end if RUBY_VERSION < '1.9' | ||
end | ||
end | ||
end | ||
# Is the integer not a multiple of 2? | ||
def odd? | ||
!even? | ||
end if RUBY_VERSION < '1.9' | ||
end |
26 changes: 10 additions & 16 deletions
26
activesupport/lib/active_support/core_ext/integer/inflections.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,14 @@ | ||
require 'active_support/inflector' | ||
|
||
module ActiveSupport #:nodoc: | ||
module CoreExtensions #:nodoc: | ||
module Integer #:nodoc: | ||
module Inflections | ||
# Ordinalize turns a number into an ordinal string used to denote the | ||
# position in an ordered sequence such as 1st, 2nd, 3rd, 4th. | ||
# | ||
# 1.ordinalize # => "1st" | ||
# 2.ordinalize # => "2nd" | ||
# 1002.ordinalize # => "1002nd" | ||
# 1003.ordinalize # => "1003rd" | ||
def ordinalize | ||
Inflector.ordinalize(self) | ||
end | ||
end | ||
end | ||
class Integer | ||
# Ordinalize turns a number into an ordinal string used to denote the | ||
# position in an ordered sequence such as 1st, 2nd, 3rd, 4th. | ||
# | ||
# 1.ordinalize # => "1st" | ||
# 2.ordinalize # => "2nd" | ||
# 1002.ordinalize # => "1002nd" | ||
# 1003.ordinalize # => "1003rd" | ||
def ordinalize | ||
Inflector.ordinalize(self) | ||
end | ||
end |