Skip to content

Instantly share code, notes, and snippets.

@keithrbennett
Created September 30, 2018 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keithrbennett/ef3a3f194fb57e436bebedda4daafb69 to your computer and use it in GitHub Desktop.
Save keithrbennett/ef3a3f194fb57e436bebedda4daafb69 to your computer and use it in GitHub Desktop.
Illustrates using built-in Ruby counter to present numbers as upper case strings including 'A' - 'Z'.
2.5.1 :022 > def fmt(n); n.to_s(26).chars.map { |c| (c.ord + 16).chr }.join; end
=> :fmt
2.5.1 :023 > def val(s); s.chars.map { |c| (c.ord - 16).chr }.join.to_i(26); end
=> :val
2.5.1 :024 > n = (26 * 26) + (2 * 26) + 3 # 'ABC'
=> 731
2.5.1 :025 > fmt n
=> "ABC"
2.5.1 :026 > val 'ABC'
=> 731
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment