如何使用Ruby打印雪人

And*_*imm 10 ruby unicode

我想要一个占位符来表示我尚未实现的单个字符串.如何在Ruby 1.9中打印雪人?

目前,我可以做到

# coding: utf-8
puts "?"
Run Code Online (Sandbox Code Playgroud)

要么

puts "\u2603"
Run Code Online (Sandbox Code Playgroud)

但是可以使用"索引条目"字段(此处提到)snowy weatherSNOWMAN或者weather, snowy打印字符吗?

我没有使用Rails.

knu*_*nut 7

您可以从unicode.org下载名称索引并将字符名称解析为哈希(或更好的数据库或类似).

然后你可以使用普通的数据访问功能.

例:

# 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
Run Code Online (Sandbox Code Playgroud)

编辑:

有一个gem unicode_utils.有了这个宝石你可以使用:

require "unicode_utils/grep"
p UnicodeUtils.grep(/^snowman$/) #=> [#<U+2603 "\u2603" SNOWMAN utf8:e2,98,83>]
Run Code Online (Sandbox Code Playgroud)

  • +1.也许你的机器过热,一旦打印出来就会融​​化雪人. (10认同)
  • @Eduardo那可以解释我笔记本电脑下的水坑;) (3认同)