class SourceMap::Offset
Public: Offset
is an immutable structure representing a position in a source file.
Attributes
Public: Get Integer column of offset
Public: Gets Integer line of offset
Public Class Methods
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
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
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
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
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