class SourceMap::Offset

Public: Offset is an immutable structure representing a position in a source file.

Attributes

column[R]

Public: Get Integer column of offset

line[R]

Public: Gets Integer line of offset

Public Class Methods

new(*args) click to toggle source

Public: Construct Offset value.

Returns Offset instance.

Calls superclass method
# File lib/source_map/offset.rb, line 10
def self.new(*args)
  case args.first
  when Offset
    args.first
  when Array
    super(*args.first)
  else
    super(*args)
  end
end
new(line, column) click to toggle source

Public: Initialize an Offset.

line - Integer line number column - Integer column number

# File lib/source_map/offset.rb, line 25
def initialize(line, column)
  @line, @column = line, column
end

Public Instance Methods

+(other) click to toggle source

Public: Shift the offset by some value.

other - An Offset to add by its line and column

Or an Integer to add by line

Returns a new Offset instance.

# File lib/source_map/offset.rb, line 41
def +(other)
  case other
  when Offset
    Offset.new(self.line + other.line, self.column + other.column)
  when Integer
    Offset.new(self.line + other, self.column)
  else
    raise ArgumentError, "can't convert #{other} into #{self.class}"
  end
end
<=>(other) click to toggle source

Public: Compare Offset to another.

Useful for determining if a position in a few is between two offsets.

other - Another Offset

Returns a negative number when other is smaller and a positive number when its greater. Implements the Comparable#<=> protocol.

# File lib/source_map/offset.rb, line 60
def <=>(other)
  case other
  when Offset
    diff = self.line - other.line
    diff.zero? ? self.column - other.column : diff
  else
    raise ArgumentError, "can't convert #{other.class} into #{self.class}"
  end
end
inspect() click to toggle source

Public: Get a pretty inspect output for debugging purposes.

Returns a String.

# File lib/source_map/offset.rb, line 84
def inspect
  "#<#{self.class} line=#{line}, column=#{column}>"
end
to_s() click to toggle source

Public: Get a simple string representation of the offset

Returns a String.

# File lib/source_map/offset.rb, line 73
def to_s
  if column == 0
    "#{line}"
  else
    "#{line}:#{column}"
  end
end