Class: Paco::Index

Inherits:
Struct
  • Object
show all
Defined in:
lib/paco/index.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#columnObject

Returns the value of attribute column

Returns:

  • (Object)

    the current value of column



2
3
4
# File 'lib/paco/index.rb', line 2

def column
  @column
end

#lineObject

Returns the value of attribute line

Returns:

  • (Object)

    the current value of line



2
3
4
# File 'lib/paco/index.rb', line 2

def line
  @line
end

#posObject

Returns the value of attribute pos

Returns:

  • (Object)

    the current value of pos



2
3
4
# File 'lib/paco/index.rb', line 2

def pos
  @pos
end

Class Method Details

.calculate(input:, pos:) ⇒ Object

Parameters:

  • input (String)
  • pos (Integer)

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
# File 'lib/paco/index.rb', line 5

def self.calculate(input:, pos:)
  raise ArgumentError, "`pos` must be a non-negative integer" if pos < 0
  raise ArgumentError, "`pos` is grater then input length" if pos > input.length

  lines = input[0..pos].lines
  line = lines.empty? ? 1 : lines.length
  column = lines.last&.length || 1
  new(pos, line, column)
end