Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a snowman using Ruby

Tags:

ruby

unicode

I want a placeholder for a single character string that I haven't implemented yet. How do I print a snowman in Ruby 1.9?

Currently, I can do

# coding: utf-8
puts "☃"

or

puts "\u2603"

but is it possible to use the "Index entries" field (mentioned here) snowy weather or SNOWMAN or weather, snowy to get the character to print?

I am not using Rails.

like image 239
Andrew Grimm Avatar asked Jul 31 '12 07:07

Andrew Grimm


1 Answers

You may download the Name Index from unicode.org and parse the characters names into a Hash (or better a DB or similiar).

Then you can get it with normal data access functions.

Example:

# coding: utf-8
index = {}
File.readlines('Index.txt').each{|line|
  line =~ /(.*)\t(.*)$/
  index[$1] = $2.to_i(16).chr("UTF-8")
}

snowman = index['SNOWMAN']
p snowman #hope it works. My shell does not show the snowman
p "\u2603" == snowman #true

Edit:

There is a gem unicode_utils. With this gem you can use:

require "unicode_utils/grep"
p UnicodeUtils.grep(/^snowman$/) #=> [#<U+2603 "\u2603" SNOWMAN utf8:e2,98,83>]
like image 156
knut Avatar answered Oct 24 '22 07:10

knut