Sha256: 8ebd750ec9fe39dbb3514385160fe89c9fe683fd7a9a4b836b7466edfda049ff

Contents?: true

Size: 994 Bytes

Versions: 7

Compression:

Stored size: 994 Bytes

Contents

module Chess
  # With this module is possible call the method {#to_utf8} on a string. This
  # method convert the chess piece identifier character into UTF8 chess
  # character.
  # @example
  #   :001 > require 'chess/utf8_notation'
  #   => true
  #   :002 > 'Qf7#'.to_utf8
  #   => '♕f7#'
  #
  # @note To use this utility explicit require is needed: <tt>require
  #   'chess/utf8_notation'</tt>
  module UTF8Notation
    # Map a piece identifier character with the corresponding UTF8 chess
    # character
    UTF8_MAP = {
      'P' => '♙',
      'R' => '♖',
      'N' => '♘',
      'B' => '♗',
      'Q' => '♕',
      'K' => '♔',
      'p' => '♟',
      'r' => '♜',
      'n' => '♞',
      'b' => '♝',
      'q' => '♛',
      'k' => '♚'
    }.freeze

    # Replace the piece identifier characters with UTF8 chess characters.
    # @return [String]
    def to_utf8
      self.gsub(/[PRNBQKprnbqk]/, UTF8_MAP)
    end
  end
end

String.prepend(Chess::UTF8Notation)

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
chess-0.4.0 lib/chess/utf8_notation.rb
chess-0.3.6 lib/chess/utf8_notation.rb
chess-0.3.5 lib/chess/utf8_notation.rb
chess-0.3.4 lib/chess/utf8_notation.rb
chess-0.3.3 lib/chess/utf8_notation.rb
chess-0.3.1 lib/chess/utf8_notation.rb
chess-0.3.0 lib/chess/utf8_notation.rb