Skip to content

Instantly share code, notes, and snippets.

@abra-chan
Created January 10, 2026 14:09
Show Gist options
  • Select an option

  • Save abra-chan/db0d671fd3ad63d1fc3f3425c50720a1 to your computer and use it in GitHub Desktop.

Select an option

Save abra-chan/db0d671fd3ad63d1fc3f3425c50720a1 to your computer and use it in GitHub Desktop.
Reverses a text file with unicode support. Flips ascii art (naively).
#!/usr/bin/env ruby
# Usage: ruby reverse_lines.rb input.txt output.txt
# This script reads each line from the input file,
# reverses the order of the characters,
# and writes the result to the output file.
# It supports ASCII and Unicode charaters (utf-8).
# The primary use-case is for manipulating ascii art, to flip the other direction.
# A more robust version would replace commonly used characters with its complement
# (E.g. the braille character ⠦ becomes ⠴ ).
# Creating such a library is out-of-scope of this project, for now.
if ARGV.length != 2
puts "Usage: ruby #{__FILE__} <input_file> <output_file>"
exit 1
end
input_file = ARGV[0]
output_file = ARGV[1]
# Ensure UTF-8 encoding for Unicode support
File.open(output_file, 'w:UTF-8') do |out|
File.foreach(input_file, encoding: 'UTF-8') do |line|
reversed_line = line.chomp.reverse + "\n" # Reverse chars, preserve trailing newline
out.write(reversed_line)
end
end
puts "Reversed lines written to #{output_file}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment