Class: Numeric

Inherits:
Object show all
Defined in:
activesupport/lib/active_support/core_ext/object/blank.rb,
activesupport/lib/active_support/core_ext/object/json.rb,
activesupport/lib/active_support/core_ext/numeric/time.rb,
activesupport/lib/active_support/core_ext/numeric/bytes.rb,
activesupport/lib/active_support/core_ext/numeric/inquiry.rb,
activesupport/lib/active_support/core_ext/object/duplicable.rb,
activesupport/lib/active_support/core_ext/string/output_safety.rb

Overview

:nodoc:

Constant Summary collapse

KILOBYTE =
1024
MEGABYTE =
KILOBYTE * 1024
GIGABYTE =
MEGABYTE * 1024
TERABYTE =
GIGABYTE * 1024
PETABYTE =
TERABYTE * 1024
EXABYTE =
PETABYTE * 1024

Instance Method Summary collapse

Instance Method Details

#as_json(options = nil) ⇒ Object

:nodoc:


94
95
96
# File 'activesupport/lib/active_support/core_ext/object/json.rb', line 94

def as_json(options = nil) #:nodoc:
  self
end

#blank?false

No number is blank:

1.blank? # => false
0.blank? # => false

Returns:

  • (false)

129
130
131
# File 'activesupport/lib/active_support/core_ext/object/blank.rb', line 129

def blank?
  false
end

#bytesObject Also known as: byte

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

2.bytes # => 2

12
13
14
# File 'activesupport/lib/active_support/core_ext/numeric/bytes.rb', line 12

def bytes
  self
end

#daysObject Also known as: day

Returns a Duration instance matching the number of days provided.

2.days # => 2 days

45
46
47
# File 'activesupport/lib/active_support/core_ext/numeric/time.rb', line 45

def days
  ActiveSupport::Duration.new(self * 24.hours, [[:days, self]])
end

#duplicable?Boolean

Numbers are not duplicable:

3.duplicable? # => false
3.dup         # => TypeError: can't dup Integer

Returns:

  • (Boolean)

74
75
76
# File 'activesupport/lib/active_support/core_ext/object/duplicable.rb', line 74

def duplicable?
  false
end

#exabytesObject Also known as: exabyte

Returns the number of bytes equivalent to the exabytes provided.

2.exabytes # => 2_305_843_009_213_693_952

60
61
62
# File 'activesupport/lib/active_support/core_ext/numeric/bytes.rb', line 60

def exabytes
  self * EXABYTE
end

#fortnightsObject Also known as: fortnight

Returns a Duration instance matching the number of fortnights provided.

2.fortnights # => 4 weeks

61
62
63
# File 'activesupport/lib/active_support/core_ext/numeric/time.rb', line 61

def fortnights
  ActiveSupport::Duration.new(self * 2.weeks, [[:weeks, self * 2]])
end

#gigabytesObject Also known as: gigabyte

Returns the number of bytes equivalent to the gigabytes provided.

2.gigabytes # => 2_147_483_648

36
37
38
# File 'activesupport/lib/active_support/core_ext/numeric/bytes.rb', line 36

def gigabytes
  self * GIGABYTE
end

#hoursObject Also known as: hour

Returns a Duration instance matching the number of hours provided.

2.hours # => 2 hours

37
38
39
# File 'activesupport/lib/active_support/core_ext/numeric/time.rb', line 37

def hours
  ActiveSupport::Duration.new(self * 3600, [[:hours, self]])
end

#html_safe?Boolean

Returns:

  • (Boolean)

129
130
131
# File 'activesupport/lib/active_support/core_ext/string/output_safety.rb', line 129

def html_safe?
  true
end

#in_millisecondsObject

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

71
72
73
# File 'activesupport/lib/active_support/core_ext/numeric/time.rb', line 71

def in_milliseconds
  self * 1000
end

#kilobytesObject Also known as: kilobyte

Returns the number of bytes equivalent to the kilobytes provided.

2.kilobytes # => 2048

20
21
22
# File 'activesupport/lib/active_support/core_ext/numeric/bytes.rb', line 20

def kilobytes
  self * KILOBYTE
end

#megabytesObject Also known as: megabyte

Returns the number of bytes equivalent to the megabytes provided.

2.megabytes # => 2_097_152

28
29
30
# File 'activesupport/lib/active_support/core_ext/numeric/bytes.rb', line 28

def megabytes
  self * MEGABYTE
end

#minutesObject Also known as: minute

Returns a Duration instance matching the number of minutes provided.

2.minutes # => 2 minutes

29
30
31
# File 'activesupport/lib/active_support/core_ext/numeric/time.rb', line 29

def minutes
  ActiveSupport::Duration.new(self * 60, [[:minutes, self]])
end

#negative?Boolean

Returns true if the number is negative.

-1.negative? # => true
0.negative?  # => false
1.negative?  # => false

Returns:

  • (Boolean)

17
18
19
# File 'activesupport/lib/active_support/core_ext/numeric/inquiry.rb', line 17

def negative?
  self < 0
end

#petabytesObject Also known as: petabyte

Returns the number of bytes equivalent to the petabytes provided.

2.petabytes # => 2_251_799_813_685_248

52
53
54
# File 'activesupport/lib/active_support/core_ext/numeric/bytes.rb', line 52

def petabytes
  self * PETABYTE
end

#positive?Boolean

Returns true if the number is positive.

1.positive?  # => true
0.positive?  # => false
-1.positive? # => false

Returns:

  • (Boolean)

8
9
10
# File 'activesupport/lib/active_support/core_ext/numeric/inquiry.rb', line 8

def positive?
  self > 0
end

#secondsObject Also known as: second

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

21
22
23
# File 'activesupport/lib/active_support/core_ext/numeric/time.rb', line 21

def seconds
  ActiveSupport::Duration.new(self, [[:seconds, self]])
end

#terabytesObject Also known as: terabyte

Returns the number of bytes equivalent to the terabytes provided.

2.terabytes # => 2_199_023_255_552

44
45
46
# File 'activesupport/lib/active_support/core_ext/numeric/bytes.rb', line 44

def terabytes
  self * TERABYTE
end

#weeksObject Also known as: week

Returns a Duration instance matching the number of weeks provided.

2.weeks # => 2 weeks

53
54
55
# File 'activesupport/lib/active_support/core_ext/numeric/time.rb', line 53

def weeks
  ActiveSupport::Duration.new(self * 7.days, [[:weeks, self]])
end