Q:

Multibyte character issue with .match?

The following code is something I am beginning to test for use within a "Texas Hold Em" style game I am working on. 
My question is why, when running the following code, does the puts involving a "♥" return a "\u" in it's place. I feel certain it is this multibyte character that is causing the issue becuse on the second puts , I replaced the ♦ with a d in the array of strings and it returned what i was expecting. See Below:
My Code:
#! /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}"

Puts to Screen:
|8\u

|6d|

-- My goal would be to get the first puts to return: |8♥|
I am not so much looking for a solution to this (there may not even be one) but more so a "as simple as possible" explanation of what is causing this issue and why.  Thanks ahead of time for any information on what is happening here and how I can tackle the goal.

A:

The "\u" you're seeing is the Unicode string indicator.
For example, Unicode character 'HEAVY BLACK HEART' (U+2764) can be printed as "\u2764".
A friendly Unicode character listing site is http://unicode-table.com/en/sets/
Are you able to launch interactive Ruby in your shell and print a heart like this?
irb
irb> puts "\u2764"
❤

When I run your code in my Ruby, I get the answer you expect:
test_str_1 = /(\D8\D{2})/.match(table_cards.to_s)
=> #<MatchData "|8♥|" 1:"|8♥|">

What happens if you try a regex that is more specific to your cards?
 test_str_1 = /(\|8[♥♦♣♠]\|)/.match(table_cards.to_s)

In your example output, you're not seeing the Unicode heart symbol as you want. Instead, your output is printing the "\u" which is the Unicode starter, but then not printing the rest of the expected string which is "2764". 

See the comment by the Tin Man that describes encoding for your console. If he's correct, then I expect the more-specific regex will succeed, but still print the wrong output.
See the comment by David Knipe that says it looks like it gets truncated because the regex only matches 4 characters. If he's correct, then I expect the more-specific regex will succeed and also print the right output.

(The rest of this answer is typical for Unix; if you're on Windows, ignore the rest here...)
To show your system language settings, try this in your shell:
echo $LC_ALL
echo $LC_CTYPE

If they are not "UTF-8" or something like that, try this in your shell:
export LC_ALL=en_US.UTF-8
export LC_CTYPE=en_US.UTF-8

Then re-run your code -- be sure to use the same shell.
If this works, and you want to make this permanent, one way is to add these here:
# /etc/environment
LC_ALL=en_US.UTF-8
LC_CTYPE=en_US.UTF-8

Then source that file from your .bashrc or .zshrc or whatever shell startup file you use.

