Project

General

Profile

Actions

Feature #7580

open

Range translation

Added by Anonymous over 11 years ago. Updated almost 6 years ago.

Status:
Assigned
Target version:
-
[ruby-core:50936]

Description

=begin
I would like to propose the (({#+})) and (({#-})) methods on (({Range})).

These would be useful for translating ranges - for example, given a range where the endpoints are 1-indexed, the range could be translated by 1 in the negative direction to use in (({Array#[]})).

Instead of doing a syntactically-bulky manual translation like so:

ary[(range.begin - 1)..(range.end - 1)]

(({Range#-})) could be used instead:

ary[range - 1]

The translation methods would not handle certain endpoint types specially, they would just pass the call on.

Here's an example implementation in Ruby:

class Range
def +(other)
Range.new(self.begin + other, self.end + other, exclude_end?)
end

def -(other)
  Range.new(self.begin - other, self.end - other, exclude_end?)
end

end

=end

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0