diff options
Diffstat (limited to 'activesupport/lib/active_support/core_ext/integer')
3 files changed, 59 insertions, 84 deletions
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 b1d1e28062..6d005268a3 100644 --- a/activesupport/lib/active_support/core_ext/integer/even_odd.rb +++ b/activesupport/lib/active_support/core_ext/integer/even_odd.rb @@ -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 diff --git a/activesupport/lib/active_support/core_ext/integer/inflections.rb b/activesupport/lib/active_support/core_ext/integer/inflections.rb index 804702beb2..e81e7af436 100644 --- a/activesupport/lib/active_support/core_ext/integer/inflections.rb +++ b/activesupport/lib/active_support/core_ext/integer/inflections.rb @@ -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 + ActiveSupport::Inflector.ordinalize(self) end end diff --git a/activesupport/lib/active_support/core_ext/integer/time.rb b/activesupport/lib/active_support/core_ext/integer/time.rb index 356e145b90..a0ccf0e971 100644 --- a/activesupport/lib/active_support/core_ext/integer/time.rb +++ b/activesupport/lib/active_support/core_ext/integer/time.rb @@ -1,45 +1,39 @@ -module ActiveSupport #:nodoc: - module CoreExtensions #:nodoc: - module Integer #:nodoc: - # Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years. - # - # These methods use Time#advance for precise date calculations when using from_now, ago, etc. - # as well as adding or subtracting their results from a Time object. For example: - # - # # equivalent to Time.now.advance(:months => 1) - # 1.month.from_now - # - # # equivalent to Time.now.advance(:years => 2) - # 2.years.from_now - # - # # equivalent to Time.now.advance(:months => 4, :years => 5) - # (4.months + 5.years).from_now - # - # While these methods provide precise calculation when used as in the examples above, care - # should be taken to note that this is not true if the result of `months', `years', etc is - # converted before use: - # - # # equivalent to 30.days.to_i.from_now - # 1.month.to_i.from_now - # - # # equivalent to 365.25.days.to_f.from_now - # 1.year.to_f.from_now - # - # In such cases, Ruby's core - # Date[http://stdlib.rubyonrails.org/libdoc/date/rdoc/index.html] and - # Time[http://stdlib.rubyonrails.org/libdoc/time/rdoc/index.html] should be used for precision - # date and time arithmetic - module Time - def months - ActiveSupport::Duration.new(self * 30.days, [[:months, self]]) - end - alias :month :months +class Integer + # Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years. + # + # These methods use Time#advance for precise date calculations when using from_now, ago, etc. + # as well as adding or subtracting their results from a Time object. For example: + # + # # equivalent to Time.now.advance(:months => 1) + # 1.month.from_now + # + # # equivalent to Time.now.advance(:years => 2) + # 2.years.from_now + # + # # equivalent to Time.now.advance(:months => 4, :years => 5) + # (4.months + 5.years).from_now + # + # While these methods provide precise calculation when used as in the examples above, care + # should be taken to note that this is not true if the result of `months', `years', etc is + # converted before use: + # + # # equivalent to 30.days.to_i.from_now + # 1.month.to_i.from_now + # + # # equivalent to 365.25.days.to_f.from_now + # 1.year.to_f.from_now + # + # In such cases, Ruby's core + # Date[http://stdlib.rubyonrails.org/libdoc/date/rdoc/index.html] and + # Time[http://stdlib.rubyonrails.org/libdoc/time/rdoc/index.html] should be used for precision + # date and time arithmetic + def months + ActiveSupport::Duration.new(self * 30.days, [[:months, self]]) + end + alias :month :months - def years - ActiveSupport::Duration.new(self * 365.25.days, [[:years, self]]) - end - alias :year :years - end - end + def years + ActiveSupport::Duration.new(self * 365.25.days, [[:years, self]]) end + alias :year :years end |