Class: Gimchi::Char

Inherits:
Object
  • Object
show all
Defined in:
lib/gimchi/char.rb

Overview

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.

Defined Under Namespace

Modules: Component

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kchar) ⇒ Char

Returns a new instance of Char.

Parameters:

  • kchar (String)

    Korean character string

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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

Instance Attribute Details

#chosungString

Returns Chosung component of this character.

Returns:

  • (String)

    Chosung component of this character.



10
11
12
# File 'lib/gimchi/char.rb', line 10

def chosung
  @chosung
end

#jongsungString

Returns Jongsung component of this character.

Returns:

  • (String)

    Jongsung component of this character.



14
15
16
# File 'lib/gimchi/char.rb', line 14

def jongsung
  @jongsung
end

#jungsungString

Returns Jungsung component of this character.

Returns:

  • (String)

    Jungsung component of this character.



12
13
14
# File 'lib/gimchi/char.rb', line 12

def jungsung
  @jungsung
end

Instance Method Details

#complete?Boolean

Checks if this is a complete Korean character.

Returns:

  • (Boolean)


80
81
82
# File 'lib/gimchi/char.rb', line 80

def complete?
  chosung.nil? == false && jungsung.nil? == false
end

#inspectObject



90
91
92
# File 'lib/gimchi/char.rb', line 90

def inspect
  "#{to_s}(#{to_a.join('/')})"
end

#partial?Boolean

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

Returns:

  • (Boolean)


86
87
88
# File 'lib/gimchi/char.rb', line 86

def partial?
  chosung.nil? || jungsung.nil?
end

#to_aArray

Returns Array of three components.

Returns:

  • (Array)

    Array of three components



75
76
77
# File 'lib/gimchi/char.rb', line 75

def to_a
  [chosung, jungsung, jongsung]
end

#to_sString

Recombines components into a korean character.

Returns:

  • (String)

    Combined korean character



43
44
45
# File 'lib/gimchi/char.rb', line 43

def to_s
  Gimchi.compose chosung, jungsung, jongsung
end