<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: codepoint code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 06:44:48 GMT</pubDate>
    <description>DZone Snippets: codepoint code</description>
    <item>
      <title>Convert Unicode codepoints to UTF-8 characters with Module#const_missing</title>
      <link>http://snippets.dzone.com/posts/show/4546</link>
      <description>From: http://www.davidflanagan.com/blog/2007_08.html#000136&lt;br /&gt;Author: David Flanagan&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;# This module lazily defines constants of the form Uxxxx for all Unicode&lt;br /&gt;# codepoints from U0000 to U10FFFF. The value of each constant is the&lt;br /&gt;# UTF-8 string for the codepoint.&lt;br /&gt;# Examples:&lt;br /&gt;#   copyright = Unicode::U00A9&lt;br /&gt;#   euro = Unicode::U20AC&lt;br /&gt;#   infinity = Unicode::U221E&lt;br /&gt;#&lt;br /&gt;module Unicode&lt;br /&gt;  def self.const_missing(name)  &lt;br /&gt;    # Check that the constant name is of the right form: U0000 to U10FFFF&lt;br /&gt;    if name.to_s =~ /^U([0-9a-fA-F]{4,5}|10[0-9a-fA-F]{4})$/&lt;br /&gt;      # Convert the codepoint to an immutable UTF-8 string,&lt;br /&gt;      # define a real constant for that value and return the value&lt;br /&gt;      #p name, name.class&lt;br /&gt;      const_set(name, [$1.to_i(16)].pack("U").freeze)&lt;br /&gt;    else  # Raise an error for constants that are not Unicode.&lt;br /&gt;      raise NameError, "Uninitialized constant: Unicode::#{name}"&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;puts copyright = Unicode::U00A9&lt;br /&gt;puts euro = Unicode::U20AC&lt;br /&gt;puts euro = Unicode::U20AC&lt;br /&gt;puts infinity = Unicode::U221E&lt;br /&gt;puts Unicode.const_get(:U221E)&lt;br /&gt;p Unicode.constants&lt;br /&gt;puts Unicode.constants&lt;br /&gt;Unicode.constants.each { |u| puts Unicode.const_get(u) }&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Sat, 15 Sep 2007 12:25:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4546</guid>
      <author>ntk ()</author>
    </item>
  </channel>
</rss>
