Created
October 31, 2012 00:45
-
-
Save anonymous/3984108 to your computer and use it in GitHub Desktop.
Struggling ruby loop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I am attempting to read all files in an input/ directory | |
# (index.html.erb, style.css.erb & script.js.erb) and | |
# compile them into their corresponding files in the | |
# output/ directory (index.html, style.css & script.js) | |
# Currently, only one file is rendering correctly and | |
# the following error is being thrown: | |
# ERB_test.rb:12:in `read': No such file or directory - script.js (Errno::ENOENT) | |
# from single_loop.rb:12:in `block in <main>' | |
# from single_loop.rb:11:in `each' | |
# from single_loop.rb:11:in `<main>' | |
require 'erb' | |
# test variables to be replaced in ERB files | |
h1_content = 'this loop' | |
h1_color = '#000' | |
p_content = 'is not doing so well' | |
p_color = '#444' | |
jQuery_element = 'p' | |
files = Dir.glob('input/*') # save "input" directory contents as local variable | |
for item in files do | |
template = File.read(item) | |
erb = ERB.new(template) | |
# remove "input/" prefix and ".erb" suffix from strings in files | |
files.map { |f| f.slice!("input/") } | |
files.map { |f| f.slice!(".erb") } | |
File.open('output/' + item, 'w') do |f| | |
f.write erb.result(binding) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment