Range within a range

Is there a way to check if a range is within the boundaries of another
range?

(1…10) === (3…6) => return false

I know I could re-open the range class and write a method that loops
through and checks with include? like this?

class Range
def range_included?(range)
range.each do |n|
if !self.include(n)
return false
end
end
true
end
end

but just wanted to see if there was something cleaner.

John S. wrote in post #1003010:

but just wanted to see if there was something cleaner.

Ruby has a Set class which has a subset?() method. The Set
constructor can take a range as an argument.

You certainly wouldn’t have to loop through all the elements of a range
to determine if it is included in another range. All you have
to do is look at the end points–that’s two comparisons for any sized
range.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs