Skip to content

Instantly share code, notes, and snippets.

@maxplomer
Last active August 29, 2015 14:04
Show Gist options
  • Save maxplomer/8335200bc78f9b87e6c2 to your computer and use it in GitHub Desktop.
Save maxplomer/8335200bc78f9b87e6c2 to your computer and use it in GitHub Desktop.
This code combines many ruby files into one document, then uploads to your github account as a public gist
#Author: Max Plomer, maxplomer@gmail.com
#About the code: This code combines many ruby files into one document, does some formatting (such as: prints each file name, removing whitespace at bottom of each file), it either removes or keeps comments. Then uploads to your github account as a public gist. Also a file called combined.rb is created in the folder that is same file uploaded to gist. The public gist is opened in your web browser. Installation of the gist gem is necessary using command: $ gem install gist
#About the input.txt file: This code uses an input file input.txt in the same directory, with first line containing the gist name, 2nd line whether or not you want comments, and the rest of the lines are the paths of the ruby files you want to combine relative to the location of this code (or just write out the entire path)
######sample input.txt file#######
#gist_name: homework_solutions
#keep_comments: false
#learn_ruby/00_hello/hello.rb
#learn_ruby/01_temperature/temperature.rb
#learn_ruby/02_calculator/calculator.rb
#learn_ruby/03_simon_says/simon_says.rb
#learn_ruby/04_pig_latin/pig_latin.rb
#learn_ruby/05_silly_blocks/silly_blocks.rb
#learn_ruby/06_performance_monitor/performance_monitor.rb
#learn_ruby/07_hello_friend/friend.rb
#learn_ruby/08_book_titles/book.rb
#learn_ruby/09_timer/timer.rb
#learn_ruby/10_temperature_object/temperature.rb
#learn_ruby/11_dictionary/dictionary.rb
#learn_ruby/12_rpn_calculator/rpn_calculator.rb
#learn_ruby/13_xml_document/xml_document.rb
#learn_ruby/14_array_extensions/array_extensions.rb
#learn_ruby/15_in_words/in_words.rb
require 'gist'
puts "homework uploader initialized"
def to_boolean(str)
str == 'true'
end
def remove_comments(hw_text)
result = []
hw_text.each do |line|
comment = false
line_array = line.split("")
line_array.each_index do |i|
comment = true if (line_array[i]=="#" && line_array[i+1]!="{")
result << line_array[i] if(!comment or line_array[i]=="\n")
end
end
result.join.split("\n")
end
####read input file###
lines = File.readlines "input.txt"
gist_name = lines.shift.split.last
keep_comments = to_boolean(lines.shift.split.last)
hw_files = lines
hw_names = []
hw_files.each { |file| hw_names << file.split("/").last }
###create combined.rb file###
filename = "combined.rb"
File.open(filename,'w') do |file_output|
hw_files.length.times do |i|
###hw problem name###
file_output.puts("#############################\n#" + hw_names[i])
###read in hw text###
hw_text = File.readlines hw_files[i].strip
###delete comments if requested###
hw_text = remove_comments(hw_text) unless keep_comments
###want to remove all blank lines after end ###
(hw_text.length-1).downto(0) do |j|
break if hw_text[j].strip == "end"
hw_text.delete_at(j) if hw_text[j].strip == ''
end
###copy hw text to combined file###
file_output.puts(hw_text)
###add blank line at bottom###
file_output.puts "\n"
end
end
Gist.login!
Gist.gist File.read("combined.rb"), public: true, filename: gist_name, open: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment