class Raheui::Code

Store code object.

Constants

FINAL_CONSONANTS
INITIAL_CONSONANTS

Count of initial, medial and final consonant of Korean alphabet.

MEDIAL_CONSONANTS

Attributes

height[R]

Returns the Integer height of code.

width[R]

Returns the Integer width of code.

Public Class Methods

new(str) click to toggle source

Initialize a Code. Separate Korean alphabet into consonants. Calculate the width and height of code.

str - The String raw text of code.

Examples

Code.new('아희')

Code.new(
  <<-CODE
밤밣따빠밣밟따뿌
빠맣파빨받밤뚜뭏
돋밬탕빠맣붏두붇
볻뫃박발뚷투뭏붖
뫃도뫃희멓뭏뭏붘
뫃봌토범더벌뿌뚜
뽑뽀멓멓더벓뻐뚠
뽀덩벐멓뻐덕더벅
  CODE
)
# File lib/raheui/code.rb, line 40
def initialize(str)
  @matrix = str.lines.map do |line|
    line.chomp.chars.map { |ch| consonants(ch) }
  end
  @height = @matrix.size
  if @height.zero?
    @width = 1
    @height = 1
  else
    @width = @matrix.map(&:size).max
  end
end

Public Instance Methods

[](x, y) click to toggle source

Get consonants of the given position.

x - The Integer position of x coordinate. y - The Integer position of y coordinate.

Examples

code = Code.new('아희')
code[0, 0]
# => [11, 0, 0]
code[1, 0]
# => [18, 19, 0]
code[0, 1]
# => []
code[1, 1]
# => []

Returns an Array consists of consonants or an empty Array when there is no

element in the position.
# File lib/raheui/code.rb, line 72
def [](x, y)
  (@matrix[y] || [])[x] || []
end

Private Instance Methods

consonants(ch) click to toggle source

Separate Korean alphabet into consonants.

ch - The String character to separate.

Examples

consonants('가')
# => [0, 0, 0]

consonants('힣')
# => [18, 20, 27]

consonants('a')
# => []

Returns an Array of index of consonants or empty Array if the character is

not an Korean alphabet.
# File lib/raheui/code.rb, line 95
def consonants(ch)
  ord = ch.ord
  if 44_032 <= ord && ord <= 55_203 # '가'..'힣'
    index = ord - 44_032
    [index / (MEDIAL_CONSONANTS * FINAL_CONSONANTS),
     (index / FINAL_CONSONANTS) % MEDIAL_CONSONANTS,
     index % FINAL_CONSONANTS]
  else
    []
  end
end