以下代码是我开始测试在我正在使用的“德州扑克Em”风格游戏中使用的内容。.match多字节字符问题?
我的问题是,当运行下面的代码时,涉及“♥”的放入函数返回一个“\ u”的位置。我感到肯定的是这个多字节字符引起了第二个问题的原因,我用字符串数组中的d替换了它,它返回了我期待的内容。见下图:
我的代码:
#! /usr/bin/env ruby
# encoding: utf-8
table_cards = ["|2♥|", "|8♥|", "|6d|", "|6♣|", "|Q♠|"]
# Array of cards
player_1_face_1 = "8"
player_1_suit_1 = "♦"
# Player 1's face and suit of first card he has
player_1_face_2 = "6"
player_1_suit_2 = "♥"
# Player 1's face and suit of second card he has
test_str_1 = /(\D8\D{2})/.match(table_cards.to_s)
# EX: Searching for match between face values on (player 1's |8♦|) and the |8♥| on the table
test_str_2 = /(\D6\D{2})/.match(table_cards.to_s)
# EX: Searching for match between face values on (player 1's |6♥|) and the |6d| on the table
puts "#{test_str_1}"
puts "#{test_str_2}"
提出到屏幕:
|8\u
|6d|
- 我的目标是拿到首先将返回:| 8♥|
我没有太多的寻找解决方案(甚至可能没有),但更多的是“尽可能简单”的解释是什么导致这个问题以及为什么。提前感谢任何有关此处发生的事情以及我如何解决目标的信息。
操作系统?什么终端? – Anthony 2015-01-09 21:47:47
将Unicode字符打印到屏幕上涉及的不仅仅是Ruby脚本的正确编码。它还要求您的控制台使用相同的代码集。如果这被设置为别的东西,那么这些事情就会发生。 Windows通常使用Win1252或ISO-8859-1,并且字符定义不匹配导致奇怪的输出。 – 2015-01-09 21:49:03
这与'match'有关吗?你尝试过'puts“♥”'吗? – Stefan 2015-01-09 22:26:01