Search code examples
rubyirb

print random unicode card in console


I'm trying to make a method that returns a random card pic in console:

def random_card
  x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'A', 'B', 'C', 'D', 'F'].shuffle.join[0]
  y = %w[A B C D].shuffle.join[0]
  card = '\u{1F0' + y.to_s + x.to_s + '}'
  puts card.to_s
end

executing this prints

\u{1F0BF}

but i need same behavior as:

puts "\u{1F0BF}"

output

🂿

Solution

  • Ok first answer was wrong I have overseen something but:

    How about this (tested):

            ... 
            hexnum="1F0#{y}#{x}"
            card=''
            card  << hexnum.to_i(16)
            ...
    

    you make the "hex" dynamically, convert it into an integer and push this as first char into string
    stolen here https://gist.github.com/O-I/6758583