Skip to content

Instantly share code, notes, and snippets.

View hosh's full-sized avatar

Ho-Sheng Hsiao hosh

  • Remine
  • Phoenix, AZ
View GitHub Profile
@hosh
hosh / example1.rb
Created August 26, 2011 18:15 — forked from defunkt/gist:2059
def explain!
puts 'Want to embed this Gist?'
puts 'Use this: <script src="http://gist.github.com/2059.js"></script>'
end
explain!
@hosh
hosh / group_by_helper.rb
Created June 29, 2011 19:55
Group By Helper
# Ugly hack until PostgreSQL 9.1 comes out.
module Models
module GroupByHelper
extend ActiveSupport::Concern
included do
scope :group_by_all, lambda { group(self.column_names.map { |c| "#{self.table_name}.#{c}" }.join(',')) }
end
end
end
#http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/
describe User do
subject { user }
let(:user) { User.new }
context "when name empty" do
it { should_not be_valid }
specify { user.save.should be_false }
end
@hosh
hosh / csv.rb
Created November 18, 2010 21:42
The worst kludge I've written in years.
# NOOP
#
# Place in RAILS_ROOT/lib/csv.rb
#
# Short Description: I dare you to remove this file.
#
# Long Description:
# This file is needed so that active_record/fixtures.rb (for AR3) can load 'csv' library. Since this file
# comes before the standard library load path, this gets loaded instead. Otherwise, Ruby will
# complain because FasterCSV has changed the class signature of CSV::Row
hhh@sparkfly-rails3 ~/work/sparkfly/benchmarks/metaprogramming $ uname -a
Linux sparkfly-rails3 2.6.31-gentoo-r10 #1 SMP Fri Mar 26 23:55:26 Local time zone must be set--see zic x86_64 Intel(R) Core(TM)2 Duo CPU P8800 @ 2.66GHz GenuineIntel GNU/Linux
hhh@sparkfly-rails3 ~/work/sparkfly/benchmarks/metaprogramming $ ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]
hhh@sparkfly-rails3 ~/work/sparkfly/benchmarks/metaprogramming $ ruby bench_meta.rb
user system total real
instance_eval/str 68.230000 0.070000 68.300000 ( 88.718078)
class_eval/str 69.370000 0.050000 69.420000 ( 93.801272)
eval/str 70.800000 0.020000 70.820000 (100.582867)
define_method/class 48.570000 0.100000 48.670000 ( 65.632684)
# app/concerns/let.rb
# Ripped from Rspec 2.0 beta.12
# I've found it so useful in Rspec, I made it into a module. Controller code with shared
# code seem to have a lot of use for this. For example, in inherited_resources, you would
# typically override model, parent, etc. to configure it. You would typically memomize it
# as well. With this, you can use a more compact let() syntax.
# (Note: This code will only work in Rails 3)
module Let
#!/usr/bin/env ruby
def hello_loop(names)
names.each do |name|
puts "Hello #{name}"
end
end
hello_loop(%w(
Gafitescu
class Forgery
class Merchant
PRODUCTS = %w(coffee drink gas donut sandwich icecream cigarette)
PROMOTION_TYPES = [
lambda {
["Buy X P get Y Q free",
[:X, rand(3)+1],
[:Y, rand(4)+1],
[:P, PRODUCTS.rand],
ActionController::Routing::Routes.draw do |map|
map.root :controller => :home, :action => :index
map.resources :servicios
map.resources :nosotros
#map.servicios '/servicios/consultorias', :controller => 'servicios', :action => 'consultoria'
map.connect "/servicios/:action", :controller => "servicios"
end
#!/bin/sh
# Configuration
#CLOUD_ETH="eth0"
CLOUD_ETH="eth1" # Standard for Rackspace Cloud
CHEFSERVER="10.177.133.40"
# Do not edit below