Class: Range
- 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
-
#*(other) ⇒ Range
Allows multiplying a range.
-
#+(other) ⇒ Range
Allows adding a to a range.
-
#-(other) ⇒ Range
Allows subtracting of a range.
-
#<(other) ⇒ Boolean
Less than to check.
-
#<=(other) ⇒ Boolean
Less than or equal to check.
-
#>(other) ⇒ Boolean
Greater than check.
-
#>=(other) ⇒ Boolean
Greater than or equal to check.
-
#round(precision = 0) ⇒ Range
Rounds a range.
-
#zero? ⇒ Boolean
Checks if the whole range is zero.
Instance Method Details
#*(other) ⇒ Range
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.
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
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.
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
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.
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.
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.
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.
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.
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.
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.
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 |