Skip to content

Instantly share code, notes, and snippets.

@ffaerber
Created April 22, 2012 15:48
Show Gist options
  • Select an option

  • Save ffaerber/2464782 to your computer and use it in GitHub Desktop.

Select an option

Save ffaerber/2464782 to your computer and use it in GitHub Desktop.
filelist to hash
require "awesome_print"
require 'json'
list = [
"compass-config.rb",
"config.yaml",
"content/00_home/00_index.md",
"content/00_home/01_work.md",
"content/01_additional services/00_index.md",
"content/01_additional services/01_writing labs.md",
"content/01_additional services/02_overview.md",
"content/01_additional services/03_conversation and presentation.md",
"content/01_additional services/04_how can we help you.md",
"content/01_writing services/00_index.md",
"content/01_writing services/01_copy editing.md",
"content/01_writing services/02_3-step process.md",
"content/01_writing services/02_overview.md",
"content/01_writing services/03_overview.md",
"content/01_writing services/03_translations.md",
"content/02_additional services/00_index.md",
]
list_hash = list.each { |f|
}.map{ |path|
path.split '/' # split to parts
}.inject({}){ |acc, path| # start with empty hash
path.inject(acc) do |acc2, dir| # for each path part, create a child of current node
acc2[dir] ||= {} # and pass it as new current node
end
acc
}
list_json = list_hash.to_json
puts list_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment