class Numeric

Parent:
Object

Constants

EXABYTE
GIGABYTE
KILOBYTE
MEGABYTE
PETABYTE
TERABYTE

Public Instance Methods

byte()
Alias for: bytes
bytes() Show source
# File activesupport/lib/active_support/core_ext/numeric/bytes.rb, line 12
def bytes
  self
end

Enables the use of byte calculations and declarations, like 45.bytes + 2.6.megabytes

2.bytes # => 2
Also aliased as: byte
day()
Alias for: days
days() Show source
# File activesupport/lib/active_support/core_ext/numeric/time.rb, line 45
def days
  ActiveSupport::Duration.days(self)
end

Returns a Duration instance matching the number of days provided.

2.days # => 2 days
Also aliased as: day
duplicable?() Show source
# File activesupport/lib/active_support/core_ext/object/duplicable.rb, line 99
def duplicable?
  false
end

Numbers are not duplicable:

3.duplicable? # => false
3.dup         # => TypeError: can't dup Integer
exabyte()
Alias for: exabytes
exabytes() Show source
# File activesupport/lib/active_support/core_ext/numeric/bytes.rb, line 60
def exabytes
  self * EXABYTE
end

Returns the number of bytes equivalent to the exabytes provided.

2.exabytes # => 2_305_843_009_213_693_952
Also aliased as: exabyte
fortnight()
Alias for: fortnights
fortnights() Show source
# File activesupport/lib/active_support/core_ext/numeric/time.rb, line 61
def fortnights
  ActiveSupport::Duration.weeks(self * 2)
end

Returns a Duration instance matching the number of fortnights provided.

2.fortnights # => 4 weeks
Also aliased as: fortnight
gigabyte()
Alias for: gigabytes
gigabytes() Show source
# File activesupport/lib/active_support/core_ext/numeric/bytes.rb, line 36
def gigabytes
  self * GIGABYTE
end

Returns the number of bytes equivalent to the gigabytes provided.

2.gigabytes # => 2_147_483_648
Also aliased as: gigabyte
hour()
Alias for: hours
hours() Show source
# File activesupport/lib/active_support/core_ext/numeric/time.rb, line 37
def hours
  ActiveSupport::Duration.hours(self)
end

Returns a Duration instance matching the number of hours provided.

2.hours # => 2 hours
Also aliased as: hour
html_safe?() Show source
# File activesupport/lib/active_support/core_ext/string/output_safety.rb, line 129
def html_safe?
  true
end
in_milliseconds() Show source
# File activesupport/lib/active_support/core_ext/numeric/time.rb, line 71
def in_milliseconds
  self * 1000
end

Returns the number of milliseconds equivalent to the seconds provided. Used with the standard time durations, like 1.hour.in_milliseconds – so we can feed them to JavaScript functions like getTime().

2.in_milliseconds # => 2_000
kilobyte()
Alias for: kilobytes
kilobytes() Show source
# File activesupport/lib/active_support/core_ext/numeric/bytes.rb, line 20
def kilobytes
  self * KILOBYTE
end

Returns the number of bytes equivalent to the kilobytes provided.

2.kilobytes # => 2048
Also aliased as: kilobyte
megabyte()
Alias for: megabytes
megabytes() Show source
# File activesupport/lib/active_support/core_ext/numeric/bytes.rb, line 28
def megabytes
  self * MEGABYTE
end

Returns the number of bytes equivalent to the megabytes provided.

2.megabytes # => 2_097_152
Also aliased as: megabyte
minute()
Alias for: minutes
minutes() Show source
# File activesupport/lib/active_support/core_ext/numeric/time.rb, line 29
def minutes
  ActiveSupport::Duration.minutes(self)
end

Returns a Duration instance matching the number of minutes provided.

2.minutes # => 2 minutes
Also aliased as: minute
negative?() Show source
# File activesupport/lib/active_support/core_ext/numeric/inquiry.rb, line 17
def negative?
  self < 0
end

Returns true if the number is negative.

-1.negative? # => true
0.negative?  # => false
1.negative?  # => false
petabyte()
Alias for: petabytes
petabytes() Show source
# File activesupport/lib/active_support/core_ext/numeric/bytes.rb, line 52
def petabytes
  self * PETABYTE
end

Returns the number of bytes equivalent to the petabytes provided.

2.petabytes # => 2_251_799_813_685_248
Also aliased as: petabyte
positive?() Show source
# File activesupport/lib/active_support/core_ext/numeric/inquiry.rb, line 8
def positive?
  self > 0
end

Returns true if the number is positive.

1.positive?  # => true
0.positive?  # => false
-1.positive? # => false
second()
Alias for: seconds
seconds() Show source
# File activesupport/lib/active_support/core_ext/numeric/time.rb, line 21
def seconds
  ActiveSupport::Duration.seconds(self)
end

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.current.advance(months: 1)
1.month.from_now

# equivalent to Time.current.advance(years: 2)
2.years.from_now

# equivalent to Time.current.advance(months: 4, years: 5)
(4.months + 5.years).from_now
Also aliased as: second
terabyte()
Alias for: terabytes
terabytes() Show source
# File activesupport/lib/active_support/core_ext/numeric/bytes.rb, line 44
def terabytes
  self * TERABYTE
end

Returns the number of bytes equivalent to the terabytes provided.

2.terabytes # => 2_199_023_255_552
Also aliased as: terabyte
week()
Alias for: weeks
weeks() Show source
# File activesupport/lib/active_support/core_ext/numeric/time.rb, line 53
def weeks
  ActiveSupport::Duration.weeks(self)
end

Returns a Duration instance matching the number of weeks provided.

2.weeks # => 2 weeks
Also aliased as: week

© 2004–2018 David Heinemeier Hansson
Licensed under the MIT License.