Sha256: f6c01531110d3de2e38a01ded69d30dbe2b1772a1f1776884d42061acaf877ee

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

#!/usr/bin/env ruby
# coding: utf-8
# Copyright muflax <mail@muflax.com>, 2011
# License: GNU GPL 3 <http://www.gnu.org/copyleft/gpl.html>

class Range
  def +(other)
    if other.is_a? Range
      (self.begin + other.begin)..(self.end + other.end)
    elsif other.is_a?  Numeric
      (self.begin + other)..(self.end + other)
    else
      raise NoMethodError
    end
  end

  def -(other)
    if other.is_a? Range
      (self.begin - other.begin)..(self.end - other.end)
    elsif other.is_a?  Numeric
      (self.begin - other)..(self.end - other)
    else
      raise NoMethodError
    end
  end

  def *(other)
    if other.is_a? Range
      (self.begin * other.begin)..(self.end * other.end)
    elsif other.is_a?  Numeric
      (self.begin * other)..(self.end * other)
    else
      raise NoMethodError
    end
  end

  def /(other)
    if other.is_a? Range
      (self.begin / other.begin.to_f)..(self.end / other.end.to_f)
    elsif other.is_a?  Numeric
      (self.begin / other.to_f)..(self.end / other.to_f)
    else
      raise NoMethodError
    end
  end

  def average
    (self.begin + self.end) / 2.0
  end

  def coerce(other)
    return self, other
  end    
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
range_math-0.2.0 lib/range_math.rb