Class: Range

Inherits:
Object
  • Object
show all
Defined in:
lib/interval.rb

Instance Method Summary collapse

Instance Method Details

#to_intervalObject

Convert to Interval.

(1.2 .. 5.3).to_interval # => Interval[1.2, 5.3]
(1 ... 4).to_interval    # => Interval[1, 3]
(1 ... 3.1).to_interval  # Fails


768
769
770
771
772
773
774
775
776
777
778
779
780
# File 'lib/interval.rb', line 768

def to_interval
  Interval[first,
    if exclude_end?
      (last - 1).succ - 1
    else
      last
    end]
rescue NoMethodError
  if exclude_end? && !last.respond_to?(:succ)
    raise Interval::Exception::OpenRight, self
  end
  raise
end