Sha256: e87b69cfb512e96812c1d3822e6051c9e254f48bba07ad47fe6147361a1a144f

Contents?: true

Size: 916 Bytes

Versions: 77

Compression:

Stored size: 916 Bytes

Contents

module Twitter
  # This module lazily defines constants of the form Uxxxx for all Unicode
  # codepoints from U0000 to U10FFFF. The value of each constant is the
  # UTF-8 string for the codepoint.
  # Examples:
  #   copyright = Unicode::U00A9
  #   euro = Unicode::U20AC
  #   infinity = Unicode::U221E
  #
  module Unicode
    CODEPOINT_REGEX = /^U_?([0-9a-fA-F]{4,5}|10[0-9a-fA-F]{4})$/

    def self.const_missing(name)
      # Check that the constant name is of the right form: U0000 to U10FFFF
      if name.to_s =~ CODEPOINT_REGEX
        # Convert the codepoint to an immutable UTF-8 string,
        # define a real constant for that value and return the value
        #p name, name.class
        const_set(name, [$1.to_i(16)].pack("U").freeze)
      else  # Raise an error for constants that are not Unicode.
        raise NameError, "Uninitialized constant: Unicode::#{name}"
      end
    end
  end

end

Version data entries

77 entries across 75 versions & 5 rubygems

Version Path
twitter-text-1.10.0 lib/twitter-text/unicode.rb
twitter-text-1.9.2 lib/twitter-text/unicode.rb
twitter-text-1.9.1 lib/twitter-text/unicode.rb
twitter-text-1.9.0 lib/twitter-text/unicode.rb
twitter-text-1.8.0 lib/twitter-text/unicode.rb
twitter-text-1.7.0 lib/twitter-text/unicode.rb
twitter-text-1.6.4 lib/twitter-text/unicode.rb
twitter-text-relative-1.6.2.3 lib/twitter-text-relative/unicode.rb
twitter-text-relative-1.6.2.pre.3 lib/twitter-text-relative/unicode.rb
twitter-text-relative-1.6.2.2 lib/twitter-text-relative/unicode.rb
twitter-text-relative-1.6.2.1 lib/twitter-text/unicode.rb
twitter-text-1.6.3 lib/twitter-text/unicode.rb
twitter-text-1.6.2 lib/twitter-text/unicode.rb
ritter-0.0.91 lib/ritter-text/unicode.rb
ritter-0.0.90 lib/ritter-text/unicode.rb
ritter-0.0.89 lib/ritter-text/unicode.rb
ritter-0.0.88 lib/twitter-text/unicode.rb
twitter-text-editted-1.0.0 lib/twitter-text/unicode.rb
twitter-text-1.6.1 lib/twitter-text/unicode.rb
twitter-text-1.6.0 lib/twitter-text/unicode.rb