Class: Range

Defined in:
lib/plist4r/mixin/ruby_stdlib.rb

Instance Methods

Instance Method Details

- (Range?) &(other_range)

The intersection of 2 ranges. Returns nil if there are no common elements.

Examples:

1..10 & 5..15 => 5..10

Returns:

  • (Range, nil)

    The intesection between 2 overlapping ranges, or zero



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/plist4r/mixin/ruby_stdlib.rb', line 133

def & other_range
  case other_range
  when Range
    intersection = []
    each do |i|
      intersection << i if other_range.include? i
    end
    result = intersection.to_ranges
    case result[0]
    when Integer
      return (result[0])..(result[0])
    when Range, nil
      return result[0]
    end
  else
    raise "unsupported type"
  end
end

- (true, false) include_range?(other_range)

Does this range wholely include other_range? (true or false)

Examples:

(3..5).include_range? (3..3)
=> true

(0..4).include_range? (6..7)
=> false

Returns:

  • (true, false)


160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/plist4r/mixin/ruby_stdlib.rb', line 160

def include_range? other_range
  case other_range
  when Range
    if other_range.first >= self.first && other_range.last <= self.last
      return true
    else
      return false
    end
  else
    raise "unsupported type"
  end
end

- (Object) size

The Range’s computed size, ie the number of elements in range.

Examples:

(3..3).size
=> 1

(0..9).size
=> 10

Returns:

  • The size of the range



125
126
127
# File 'lib/plist4r/mixin/ruby_stdlib.rb', line 125

def size
  last - first + 1
end