Class: Range

Inherits:
Object show all
Defined in:
lib/ruby-rails-extensions/extensions/range_add.rb,
lib/ruby-rails-extensions/extensions/zero_range.rb,
lib/ruby-rails-extensions/extensions/range_round.rb,
lib/ruby-rails-extensions/extensions/range_multiply.rb,
lib/ruby-rails-extensions/extensions/range_subtract.rb,
lib/ruby-rails-extensions/extensions/range_comparisons.rb

Instance Method Summary collapse

Instance Method Details

#*(other) ⇒ Range

Note:

Make sure to assign this to a new var or do inline replacement *=.

Allows multiplying a range. This will create a new range due to range objects being immutable.

Parameters:

Returns:

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby-rails-extensions/extensions/range_multiply.rb', line 14

def *(other)
  unless self.begin.is_a?(Numeric)
    raise(
      RubyRailsExtensions::Errors::NonNumericError,
      "Wrong range type (given #{self.begin.class}, expected Numeric)"
    )
  end

  new_begin, new_end =
    if other.is_a?(Range)
      unless other.begin.is_a?(Numeric)
        raise(
          RubyRailsExtensions::Errors::NonNumericError,
          "Wrong range type (given #{other.begin.class}, expected Numeric)"
        )
      end

      [self.begin * other.begin, self.end * other.end]
    else
      unless other.is_a?(Numeric)
        raise(
          RubyRailsExtensions::Errors::NonNumericError,
          "Wrong type (given #{other.class}, expected Numeric)"
        )
      end

      [self.begin * other, self.end * other]
    end

  Range.new(new_begin, new_end, exclude_end?)
end

#+(other) ⇒ Range

Note:

Make sure to assign this to a new var or inline replacement +=.

Allows adding a to a range. This will create a new range since ranges are immutable.

Parameters:

Returns:

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby-rails-extensions/extensions/range_add.rb', line 14

def +(other)
  unless self.begin.is_a?(Numeric)
    raise(
      RubyRailsExtensions::Errors::NonNumericError,
      "Wrong range type (given #{self.begin.class}, expected Numeric)"
    )
  end

  new_begin, new_end =
    if other.is_a?(Range)
      unless other.begin.is_a?(Numeric)
        raise(
          RubyRailsExtensions::Errors::NonNumericError,
          "Wrong range type (given #{other.begin.class}, expected Numeric)"
        )
      end

      [self.begin + other.begin, self.end + other.end]
    else
      unless other.is_a?(Numeric)
        raise(
          RubyRailsExtensions::Errors::NonNumericError,
          "Wrong type (given #{other.class}, expected Numeric)"
        )
      end

      [self.begin + other, self.end + other]
    end

  Range.new(new_begin, new_end, exclude_end?)
end

#-(other) ⇒ Range

Note:

A new range will be created since ranges are immutable. This means you need to use new_range = old_range - range/number or current_range -= range/number.

Allows subtracting of a range.

Parameters:

Returns:

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby-rails-extensions/extensions/range_subtract.rb', line 18

def -(other)
  unless self.begin.is_a?(Numeric)
    raise(
      RubyRailsExtensions::Errors::NonNumericError,
      "Wrong range type (given #{self.begin.class}, expected Numeric)"
    )
  end

  new_begin, new_end =
    if other.is_a?(Range)
      unless other.begin.is_a?(Numeric)
        raise(
          RubyRailsExtensions::Errors::NonNumericError,
          "Wrong range type (given #{other.begin.class}, expected Numeric)"
        )
      end

      [self.begin - other.begin, self.end - other.end]
    else
      unless other.is_a?(Numeric)
        raise(
          RubyRailsExtensions::Errors::NonNumericError,
          "Wrong type (given #{other.class}, expected Numeric)"
        )
      end

      [self.begin - other, self.end - other]
    end

  Range.new(new_begin, new_end, exclude_end?)
end

#<(other) ⇒ Boolean

Less than to check.

Parameters:

Returns:

  • (Boolean)


30
31
32
# File 'lib/ruby-rails-extensions/extensions/range_comparisons.rb', line 30

def <(other)
  self.begin < other
end

#<=(other) ⇒ Boolean

Less than or equal to check.

Parameters:

Returns:

  • (Boolean)


40
41
42
# File 'lib/ruby-rails-extensions/extensions/range_comparisons.rb', line 40

def <=(other)
  self.begin <= other
end

#>(other) ⇒ Boolean

Greater than check.

Parameters:

Returns:

  • (Boolean)


10
11
12
# File 'lib/ruby-rails-extensions/extensions/range_comparisons.rb', line 10

def >(other)
  self.end > other
end

#>=(other) ⇒ Boolean

Greater than or equal to check.

Parameters:

Returns:

  • (Boolean)


20
21
22
# File 'lib/ruby-rails-extensions/extensions/range_comparisons.rb', line 20

def >=(other)
  self.end >= other
end

#round(precision = 0) ⇒ Range

Rounds a range. Rounds the beginning and ending values only and creates a new range with the rounded values.

Parameters:

  • precision (Integer) (defaults to: 0)

Returns:



11
12
13
# File 'lib/ruby-rails-extensions/extensions/range_round.rb', line 11

def round(precision = 0)
  Range.new(self.begin.round(precision), self.end.round(precision), exclude_end?)
end

#zero?Boolean

Checks if the whole range is zero.

Returns:

  • (Boolean)

Raises:



10
11
12
13
14
15
16
17
18
19
# File 'lib/ruby-rails-extensions/extensions/zero_range.rb', line 10

def zero?
  unless self.begin.is_a?(Numeric)
    raise(
      RubyRailsExtensions::Errors::NonNumericError,
      "Wrong range type (given #{self.begin.class}, expected Numeric)"
    )
  end

  self.begin.zero? && self.end.zero?
end