Skip to content

Instantly share code, notes, and snippets.

@ghsyeung
ghsyeung / controller-spec.rb
Created March 21, 2012 03:13
Starting a RoR project
require 'spec-helper'
describe UserController do
describe "Search user with name" do
it "should respond to search user route"
it "should call User model method to search user with same name"
it "should show view with the list of matching users" do
response.should render_template("matching_users")
it "should redirect to index page if not found" do
response.should redirect_to(:action => )
@ghsyeung
ghsyeung / config.ru
Created March 5, 2012 22:04
Rails Lightweight Stack. Most of this is detailed on Crafting Rails Applications - http://pragprog.com/book/jvrails/crafting-rails-applications
# Run this file with `RAILS_ENV=production rackup -p 3000 -s thin`
# Be sure to have rails and thin installed.
require "rubygems"
# We are not loading Active Record, nor the Assets Pipeline, etc.
# This could also be in your Gemfile.
gem "actionpack", "~> 3.2"
gem "railties", "~> 3.2"
# The following lines should come as no surprise. Except by
@ghsyeung
ghsyeung / gist:1906078
Created February 25, 2012 03:41
Reverse a string
a = "12345"
# a[i:j:k] from i to j in steps k
s = a[::-1]
print s
# '54321'
@ghsyeung
ghsyeung / gist:1905475
Created February 25, 2012 02:51
Multi-dimensional Array Initilization
k = 1
r = 3
c = 5
# Correct way
A = [[ k ] * c for i in range(r)]
A[0][3] = 0
#>>> A
#[[1, 1, 1, 0, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]
@ghsyeung
ghsyeung / ShutdownListener.java
Created February 22, 2012 17:45
Webapp clean-up
public class ShutdownListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent sce) {
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
P p = (P) ctx.getBean("proc");
p.shutdown();
}
}