class Range
Copyright muflax <mail@muflax.com>, 2011 License: GNU GPL 3 <www.gnu.org/copyleft/gpl.html>
Public Instance Methods
%(other)
click to toggle source
# File lib/range_math.rb, line 12 def %(other) ; pair_or_num :%, other ; end
*(other)
click to toggle source
# File lib/range_math.rb, line 9 def *(other) ; pair_or_num :*, other ; end
**(other)
click to toggle source
# File lib/range_math.rb, line 11 def **(other) ; pair_or_num :**, other ; end
+(other)
click to toggle source
# File lib/range_math.rb, line 7 def +(other) ; pair_or_num :+, other ; end
-(other)
click to toggle source
# File lib/range_math.rb, line 8 def -(other) ; pair_or_num :-, other ; end
/(other)
click to toggle source
# File lib/range_math.rb, line 10 def /(other) ; pair_or_num :/, other.to_f ; end
average()
click to toggle source
# File lib/range_math.rb, line 21 def average (self.begin + self.end) / 2.0 end
coerce(other)
click to toggle source
Calls superclass method
# File lib/range_math.rb, line 25 def coerce(other) case other when Range ; return other, self when Numeric ; return (other..other), self else super end end
round(places=0)
click to toggle source
# File lib/range_math.rb, line 17 def round places=0 (self.begin.round(places)..self.end.round(places)) end
to_f()
click to toggle source
# File lib/range_math.rb, line 14 def to_f ; (self.begin.to_f..self.end.to_f) ; end
to_i()
click to toggle source
# File lib/range_math.rb, line 15 def to_i ; (self.begin.round..self.end.round) ; end
Private Instance Methods
calc_pairs(operator, other)
click to toggle source
# File lib/range_math.rb, line 45 def calc_pairs operator, other results = [ self.begin .send(operator, other.begin), self.begin .send(operator, other.end), self.end .send(operator, other.begin), self.end .send(operator, other.end), ] (results.min..results.max) end
pair_or_num(operator, other)
click to toggle source
# File lib/range_math.rb, line 36 def pair_or_num operator, other case other when Range ; calc_pairs operator, other when Numeric ; calc_pairs operator, (other..other) else raise NoMethodError end end