Skip to content

Commit

Permalink
range works
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianmandrup committed Sep 25, 2012
1 parent 588f49b commit 65151c2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions .rvmrc
@@ -0,0 +1 @@
rvm use 1.9.2@sugar-high --create
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,6 +1,6 @@
language: ruby
rvm:
- 1.8.7
- 1.9.2
- 1.9.3
script: "rake"
gemfile:
Expand Down
2 changes: 2 additions & 0 deletions README.textile
Expand Up @@ -16,6 +16,8 @@ h2. Update Aug 21, 2011

Extracted *file_mutate* and *sweetloader* gems.

Only tested for Ruby 1.9.2 and 1.9.3. Using *Travis CI*.

h2. Sugar packs

* alias
Expand Down
18 changes: 16 additions & 2 deletions lib/sugar-high/range.rb
@@ -1,12 +1,26 @@
class Range
def intersection(other)
raise ArgumentError, 'value must be a Range' unless other.kind_of?(Range)

min, max = first, exclude_end? ? max : last
other_min, other_max = other.first, other.exclude_end? ? other.max : other.last

new_min = self === other_min ? other_min : other === min ? min : nil
new_max = self === other_max ? other_max : other === max ? max : nil

new_min && new_max ? new_min..new_max : nil
end

def intersect(other)
raise ArgumentError, 'value must be a Range' unless other.kind_of?(Range)

new_min = self.cover?(other.min) ? other.min : other.cover?(min) ? min : nil
new_max = self.cover?(other.max) ? other.max : other.cover?(max) ? max : nil

new_min && new_max ? new_min..new_max : nil

rescue # if above doesn't work for ruby version
intersection(other)
end
alias_method :intersection, :intersect
alias_method :&, :intersection
alias_method :&, :intersect
end
2 changes: 1 addition & 1 deletion lib/sugar-high/version.rb
@@ -1,3 +1,3 @@
module SugarHigh
VERSION = '0.7.1'
VERSION = '0.7.2'
end

0 comments on commit 65151c2

Please sign in to comment.