9 Sep 2010, 5:58pm
ruby:
by

leave a comment

Running Gem Server to View Docs for Ruby Libraries on Localhost

gem server

will boot up documentation on port 8808 by default pass it the -p flag followed by the port number to change.

gem server -p3000
26 Aug 2010, 8:44pm
ruby:
by

1 comment

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
20 Feb 2010, 8:08pm
ruby:
by

leave a comment

Ruby Strftime Day Without the Leading Zero

%e # rather than %d
Time.now.strftime("%e")
15 Dec 2009, 11:21pm
ruby:
by

1 comment

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, :Port => 8888

Check out http://m.onkey.org/2008/11/17/ruby-on-rack-1

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)}
13 Nov 2009, 1:43pm
ruby:
by

leave a comment

How to Install a Ruby Package without Ri or RDoc

gem install --no-rdoc --no-ri rails