Class: Gimchi::Char
- Inherits:
-
Object
- Object
- Gimchi::Char
- 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
-
#chosung ⇒ String
Chosung component of this character.
-
#jongsung ⇒ String
Jongsung component of this character.
-
#jungsung ⇒ String
Jungsung component of this character.
Instance Method Summary collapse
-
#complete? ⇒ Boolean
Checks if this is a complete Korean character.
-
#initialize(kchar) ⇒ Char
constructor
A new instance of Char.
- #inspect ⇒ Object
-
#partial? ⇒ Boolean
Checks if this is a non-complete Korean character.
-
#to_a ⇒ Array
Returns Array of three components.
-
#to_s ⇒ String
Recombines components into a korean character.
Constructor Details
#initialize(kchar) ⇒ Char
Returns a new instance of Char.
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
#chosung ⇒ String
Returns Chosung component of this character.
10 11 12 |
# File 'lib/gimchi/char.rb', line 10 def chosung @chosung end |
#jongsung ⇒ String
Returns Jongsung component of this character.
14 15 16 |
# File 'lib/gimchi/char.rb', line 14 def jongsung @jongsung end |
#jungsung ⇒ String
Returns 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.
80 81 82 |
# File 'lib/gimchi/char.rb', line 80 def complete? chosung.nil? == false && jungsung.nil? == false end |
#inspect ⇒ Object
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. ㅇ, ㅏ
86 87 88 |
# File 'lib/gimchi/char.rb', line 86 def partial? chosung.nil? || jungsung.nil? end |
#to_a ⇒ Array
Returns Array of three components.
75 76 77 |
# File 'lib/gimchi/char.rb', line 75 def to_a [chosung, jungsung, jongsung] end |