Using old ruby and irb on macOS

Typing multi-byte UTF-8 Unicode characters in irb

When you try to type a multi-byte UTF-8 character like “🐳” in the default system version of irb on macOS, it may mangle the input like this:

$ irb
irb(main):001:0> "\U+FFF0"\U+FFF0\U+FFB3

To work around the problem, you can start irb with irb --noreadline:

$ irb --noreadline
irb(main):001:0> "🐳".bytes
=> [240, 159, 144, 179]

Local web server with pretty URLs

#!/usr/bin/ruby

# Serve the current working directory on a local web server with pretty URLs.
# This works like `ruby -run -e httpd . -p 8000` plus pretty URL support.
#
# On macOS Mojave with ruby 2.3.0, `require 'webrick'` is slow because of a DNS
# lookup for the mac's Bonjour local hostname (e.g. Macintosh.local). Ruby 2.4
# fixed the problem in webrick/config.rb by deferring the DNS lookup that used
# to be triggered by `:ServerName = Utils:getservername`.
#
# To make the DNS lookup much faster for the Bonjour local hostname on macOS,
# add these 2 lines to /etc/hosts:
#     127.0.0.1       Macintosh.local
#     ::1             Macintosh.local
#

require 'webrick'
config = {:Port => 8000, :DocumentRoot => Dir.pwd, :DoNotReverseLookup => true}
server = WEBrick::HTTPServer.new(config)
trap 'INT' do server.shutdown end

# Override WEBrick's FileHandler.search_file to enable pretty URLs.
# First try the normal search_file. If it fails, add a ".html" and try again.
class PrettyURLHandler < WEBrick::HTTPServlet::FileHandler
  def search_file(req, res, basename)
    result = super(req, res, basename)
    return result ? result : super(req, res, basename + ".html")
  end
end

# Start a web server on http://localhost:8000/
server.mount('/', PrettyURLHandler, config[:DocumentRoot], {})
server.start

6/27/2020