Skip to content

Instantly share code, notes, and snippets.

@doloopwhile
Last active August 29, 2015 14:09
Show Gist options
  • Save doloopwhile/8a2e742d98fbad227606 to your computer and use it in GitHub Desktop.
Save doloopwhile/8a2e742d98fbad227606 to your computer and use it in GitHub Desktop.
Pretify YAML
#!/usr/bin/env ruby
require 'yaml'
require 'pp'
ARGV.each do |a|
y = open(a) do |f|
YAML.load(f)
end
sort = lambda do |x|
if x.is_a? Hash
Hash[x.map{|k,v| [k, sort[v]]}.sort]
else
x
end
end
y = sort[y]
open(a, 'w') do |f|
y.to_yaml.lines.each do |line|
f.puts line.rstrip
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment