Class: Yosina::Char

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

Overview

Represents a character with metadata for transliteration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(c:, offset:, source: nil) ⇒ Char

Initialize a new character

rubocop:disable Naming/MethodParameterName

Parameters:

  • c (String)

    The character string

  • offset (Integer)

    The offset position in the original text

  • source (Char, nil) (defaults to: nil)

    Optional reference to the original character



14
15
16
17
18
# File 'lib/yosina/char.rb', line 14

def initialize(c:, offset:, source: nil)
  @c = c
  @offset = offset
  @source = source
end

Instance Attribute Details

#cObject

Returns the value of attribute c.



6
7
8
# File 'lib/yosina/char.rb', line 6

def c
  @c
end

#offsetObject

Returns the value of attribute offset.



6
7
8
# File 'lib/yosina/char.rb', line 6

def offset
  @offset
end

#sourceObject

Returns the value of attribute source.



6
7
8
# File 'lib/yosina/char.rb', line 6

def source
  @source
end

Instance Method Details

#==(other) ⇒ Object



51
52
53
54
55
# File 'lib/yosina/char.rb', line 51

def ==(other)
  return false unless other.is_a?(Char)

  c == other.c && offset == other.offset && source == other.source
end

#inspectObject



61
62
63
# File 'lib/yosina/char.rb', line 61

def inspect
  "#<Yosina::Char c=#{c.inspect} offset=#{offset} source=#{source&.inspect}>"
end

#sentinel?Boolean

Check if the character is a sentinel (empty character)

Returns:

  • (Boolean)

    true if the character is empty, false otherwise



24
25
26
# File 'lib/yosina/char.rb', line 24

def sentinel?
  @c.empty?
end

#to_sObject



57
58
59
# File 'lib/yosina/char.rb', line 57

def to_s
  c
end

#transliterated?Boolean

Check if the character has been transliterated

Returns:

  • (Boolean)

    true if the character has a source, false otherwise



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/yosina/char.rb', line 39

def transliterated?
  c = self
  loop do
    s = c.source
    break if s.nil?
    return true if c.c != s.c

    c = s
  end
  false
end

#with_offset(offset) ⇒ Char

Create a new Char with a different offset

Parameters:

  • offset (Integer)

    The new offset for the character

Returns:

  • (Char)

    A new Char instance with the updated offset



32
33
34
# File 'lib/yosina/char.rb', line 32

def with_offset(offset)
  Char.new(c: @c, offset: offset, source: self)
end