class Lexoranking::Main

Main class that calculates the ranking value based on some previous and next elements.

Constants

MAX_CHAR
MIN_CHAR

Attributes

after[RW]
prev[RW]

Public Class Methods

new(prev, after) click to toggle source
# File lib/lexoranking/main.rb, line 18
def initialize(prev, after)
  @prev = prev || MIN_CHAR
  @after = after || MAX_CHAR
end
perform(prev, after) click to toggle source
# File lib/lexoranking/main.rb, line 13
def perform(prev, after)
  new(prev, after).calculate_ranking
end

Public Instance Methods

calculate_ranking() click to toggle source
# File lib/lexoranking/main.rb, line 23
def calculate_ranking
  rank = ""
  i = 0

  loop do
    prev_char = get_char(prev, i, MIN_CHAR)
    after_char = get_char(after, i, MAX_CHAR)

    if prev_char == after_char
      rank += prev_char
      i += 1
      next
    end

    mid_char = mid(prev_char, after_char)
    if mid_char == prev_char || mid_char == after_char
      rank += prev_char
      i += 1
      next
    end

    rank += mid_char
    break
  end

  rank
end
get_char(str, idx, default_char) click to toggle source
# File lib/lexoranking/main.rb, line 56
def get_char(str, idx, default_char)
  idx >= str.length ? default_char : str[idx]
end
mid(prev, after) click to toggle source
# File lib/lexoranking/main.rb, line 51
def mid(prev, after)
  middle_ascii = ((prev.ord + after.ord) / 2).round
  middle_ascii.chr
end