Class: Raheui::Code
- Inherits:
-
Object
- Object
- Raheui::Code
- Extended by:
- Forwardable
- Defined in:
- lib/raheui/code.rb
Overview
Store code object.
Constant Summary collapse
- INITIAL_CONSONANTS =
Count of initial, medial and final consonant of Korean alphabet.
19- MEDIAL_CONSONANTS =
21- FINAL_CONSONANTS =
28
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the Integer height of code.
-
#width ⇒ Object
readonly
Returns the Integer width of code.
Instance Method Summary collapse
-
#[](x, y) ⇒ Object
Get consonants of the given position.
-
#initialize(str) ⇒ Code
constructor
Initialize a Code.
Constructor Details
#initialize(str) ⇒ Code
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(
"\uBC24\uBC23\uB530\uBE60\uBC23\uBC1F\uB530\uBFCC\n\uBE60\uB9E3\uD30C\uBE68\uBC1B\uBC24\uB69C\uBB4F\n\uB3CB\uBC2C\uD0D5\uBE60\uB9E3\uBD8F\uB450\uBD87\n\uBCFB\uBAC3\uBC15\uBC1C\uB6B7\uD22C\uBB4F\uBD96\n\uBAC3\uB3C4\uBAC3\uD76C\uBA53\uBB4F\uBB4F\uBD98\n\uBAC3\uBD0C\uD1A0\uBC94\uB354\uBC8C\uBFCC\uB69C\n\uBF51\uBF40\uBA53\uBA53\uB354\uBC93\uBED0\uB6A0\n\uBF40\uB369\uBC90\uBA53\uBED0\uB355\uB354\uBC85\n CODE\n)\n"
40 41 42 43 44 45 46 47 48 49 50 51 |
# 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 |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the Integer height of code.
17 18 19 |
# File 'lib/raheui/code.rb', line 17 def height @height end |
#width ⇒ Object (readonly)
Returns the Integer width of code.
14 15 16 |
# File 'lib/raheui/code.rb', line 14 def width @width end |
Instance Method Details
#[](x, y) ⇒ Object
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.
72 73 74 |
# File 'lib/raheui/code.rb', line 72 def [](x, y) (@matrix[y] || [])[x] || [] end |