class Gimchi::Char

Class representing each Korean character. Its three components, ‘chosung’, ‘jungsung’ and ‘jongsung’ can be get and set.

‘to_s’ merges components into a String. ‘to_a’ returns the three components.

Attributes

chosung[R]

@return [String] Chosung component of this character.

jongsung[R]

@return [String] Jongsung component of this character.

jungsung[R]

@return [String] Jungsung component of this character.

Public Class Methods

new(kchar) click to toggle source

@param [String] kchar Korean character string

# File lib/gimchi/char.rb, line 17
def initialize kchar
  raise ArgumentError.new('Not a korean character') unless Gimchi.korean_char? kchar

  if Gimchi.complete_korean_char? kchar
    c = kchar.unpack('U').first
    n = c - 0xAC00
    # '가' ~ '깋' -> 'ㄱ'
    n1 = n / (21 * 28)
    # '가' ~ '깋'에서의 순서
    n = n % (21 * 28)
    n2 = n / 28;
    n3 = n % 28;
    self.chosung = Gimchi.chosungs[n1]
    self.jungsung = Gimchi.jungsungs[n2]
    self.jongsung = ([nil] + Gimchi.jongsungs)[n3]
  elsif Gimchi.chosung? kchar
    self.chosung = kchar
  elsif Gimchi.jungsung? kchar
    self.jungsung = kchar
  elsif Gimchi.jongsung? kchar
    self.jongsung = kchar
  end
end

Public Instance Methods

chosung=(c) click to toggle source

Sets the chosung component. @param [String]

# File lib/gimchi/char.rb, line 49
def chosung= c
  raise ArgumentError.new('Invalid chosung component') if
      c && Gimchi.chosung?(c) == false
  @chosung = c && c.dup.extend(Component).tap { |e| e.kor = Gimchi }
end
complete?() click to toggle source

Checks if this is a complete Korean character.

# File lib/gimchi/char.rb, line 80
def complete?
  chosung.nil? == false && jungsung.nil? == false
end
inspect() click to toggle source
# File lib/gimchi/char.rb, line 90
def inspect
  "#{to_s}(#{to_a.join('/')})"
end
jongsung=(c) click to toggle source

Sets the jongsung component

@param [String]

# File lib/gimchi/char.rb, line 66
def jongsung= c
  raise ArgumentError.new('Invalid jongsung component') if
      c && Gimchi.jongsung?(c) == false
  @jongsung = c && c.dup.extend(Component).tap { |e| e.kor = Gimchi }
end
jungsung=(c) click to toggle source

Sets the jungsung component @param [String]

# File lib/gimchi/char.rb, line 57
def jungsung= c
  raise ArgumentError.new('Invalid jungsung component') if
      c && Gimchi.jungsung?(c) == false
  @jungsung = c && c.dup.extend(Component).tap { |e| e.kor = Gimchi }
end
partial?() click to toggle source

Checks if this is a non-complete Korean character. e.g. ㅇ, ㅏ

# File lib/gimchi/char.rb, line 86
def partial?
  chosung.nil? || jungsung.nil?
end
to_a() click to toggle source

Returns Array of three components.

@return [Array] Array of three components

# File lib/gimchi/char.rb, line 75
def to_a
  [chosung, jungsung, jongsung]
end
to_s() click to toggle source

Recombines components into a korean character. @return [String] Combined korean character

# File lib/gimchi/char.rb, line 43
def to_s
  Gimchi.compose chosung, jungsung, jongsung
end