Skip to content

@nate /gist:1473552
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A script to auto-generate scratch files
#!/usr/bin/env ruby
# Setup:
# 1) run: $EDITOR /some/bin/dir/scratch
# My path is /Users/nate/bin/scratch
# 2) Paste this into the file
# 3) run: chmod +x /some/bin/dir/scratch
# 4) run: scratch -h
class Scratch
def self.itch
new.itch
end
def itch
if ARGV.last == "-h"
puts
puts "Scratch - scratch paper for ruby code"
puts " scratch -h shows you this information"
puts " scratch -l returns the filename of your latest scratch file"
puts " (useful for: ruby `scratch -l`)"
puts " scratch -n starts a new scratch file"
puts " scratch -a opens entire scratch directory in your editor"
puts
elsif ARGV.last == "-n"
edit(new_filename)
elsif ARGV.last == "-a"
edit_scratch_home
elsif ARGV.last == "-l"
print "#{scratch_home}/#{last_scratch_file}"
elsif last_scratch_file
edit(last_scratch_file)
else
edit(new_filename)
end
end
def scratch_home
@scratch_home ||= "#{ENV['HOME']}/.scratch"
end
def now
@now ||= Time.now
end
def editor
@editor ||= ENV['EDITOR'] || "mate"
end
def new_filename
@new_filename ||= "#{now.strftime("%Y.%m.%d-%H:%M:%S.#{now.usec.to_s.rjust(6, "0")}")}.rb"
end
def last_scratch_file
if @last_scratch_file
@last_scratch_file
else
create_scratch_home
@last_scratch_file = `ls #{scratch_home}`.split("\n").last
end
end
def create_scratch_home
`mkdir -p #{scratch_home}`
end
def initialize_file(file)
create_scratch_home
if !File.exists?("#{scratch_home}/#{file}")
`touch #{scratch_home}/#{file}`
`echo "#!/usr/bin/env ruby\n# Created: #{now}" > #{scratch_home}/#{file}`
end
end
def edit_scratch_home
edit(nil)
end
def edit(file)
initialize_file(file)
fork { `#{editor} #{scratch_home}/#{file}` }
end
end
Scratch.itch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.