Repost
This post is inspired by
http://pupeno.com/blog/really-resetting-the-database/#comment-1179. But as my blog mostly serves as a reference for my future self, I’d like to reprint this little snippet of code here as well.
namespace :db do
desc "Crush and burn the database"
task :hard_reset => :environment do
File.delete("db/schema.rb")
Rake::Task["db:drop"].execute
Rake::Task["db:create"].execute
Rake::Task["db:migrate"].execute
Rake::Task["db:seed"].execute
if !Rails.env.test?
Rake::Task["db:data"].execute
end
end
desc "Generate sample data for developing"
task :data => :environment do
# Create the sample data in here
end
end
Ruby Strftime Day Without the Leading Zero
%e # rather than %d
Time.now.strftime("%e")
Hello Rack
What is Rack?
Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.
- Rack API Docs
Create and name your file config.ru If rack isn’t installed get it with this command
gem install rack
In your config.ru file
require 'rubygems'
require 'rack'
class HelloRack
def call(env)
[200, {"Content-Type" => "text/html"}, "Hello Rack!"]
end
end
Rack::Handler::Mongrel.run HelloRack.new,
ort => 8888
Reading, Writing, Removing Files and Directories in Ruby
These aren’t all of them, but I think they are some of the most useful.
# making a directory in another directory that doesn't yet exist...
FileUtils.mkdir_p '/path/to/your/directory/that/doesnt/exist/yet'
# recursively remove a directory and the contents
FileUtils.rm_rf("/path/to/directory/you/want/to/delete")
# write a file from the contents of another file...
File.open("/path/to/the/file.ext", "wb") {|f| f.write(@your_other_file.read)}
How to Install a Ruby Package without Ri or RDoc
gem install --no-rdoc --no-ri rails