# frozen_string_literal: true # sample-utf8.rb: UTF-8 test file for Ruby. # Free sample from 99helpers.com/file-types/rb # Run: ruby sample-utf8.rb # If any line below looks garbled (é, ’), something read this file in the # wrong encoding. Ruby itself reads source files as UTF-8 by default. # 1. No "# encoding: utf-8" comment needed: UTF-8 is the default source # encoding (since Ruby 2.0). __ENCODING__ shows what this file uses. puts "Source encoding: #{__ENCODING__}" # 2. Make output UTF-8 even where the console expects something else. $stdout.set_encoding(Encoding::UTF_8) GREETINGS = { "Dutch" => "Goedemorgen, café-eigenaar", "German" => "Grüß Gott, Straße", "French" => "Ça va, Zoë ? Très bien", "Greek" => "Καλημέρα", "Russian" => "Здравствуйте", "Arabic" => "مرحبا", "Hebrew" => "שלום", "Hindi" => "नमस्ते", "Japanese" => "こんにちは", "Chinese" => "你好", "Korean" => "안녕하세요", "Symbols" => "€ £ ¥ ₹ ≤ ≥ ∞ ✓", "Emoji" => "👋🌍💎" }.freeze # 3. length counts characters; bytesize counts the bytes on disk. GREETINGS.each do |language, text| puts format("%-9s %s (%d characters, %d bytes)", language, text, text.length, text.bytesize) end # 4. Case mapping covers all of Unicode: "ß" becomes "SS". puts "Straße".upcase # 5. Normalisation: "é" as one code point, or "e" plus a combining accent. composed = "\u00e9" decomposed = "e\u0301" puts composed == decomposed, composed == decomposed.unicode_normalize(:nfc) # 6. Method and variable names can be non-ASCII, though few projects do it. def grüße(name) = "Grüße, #{name}!" puts grüße("Ada")