Skip to content

Instantly share code, notes, and snippets.

View marshallshen's full-sized avatar

Marshall Shen marshallshen

View GitHub Profile
@marshallshen
marshallshen / gist:3981374
Created October 30, 2012 16:36
Alternative way of convert values to strings in a hash
# Old way
hash = {
"starts_at" => Date.today.to_s,
"ends_at" => (Date.today + 2.days).to_s
}
# Alternative way
hash = {
"starts_at" => Date.today,
"ends_at" => Date.today + 2.days
}
@marshallshen
marshallshen / gist:4088024
Created November 16, 2012 15:08
Create file directory level .gitignore
cd .git/info/exclude
# add the file I want to exclude.
@marshallshen
marshallshen / gist:4106298
Created November 18, 2012 17:12
Chicago Startup weekend 2012 random hack collection
Spree::Product.all.each do |product|
if product.id%2 == 0
product.send(:update_attributes!, name: "casual female style")
product.send(:update_attributes!, meta_description: "https://dl.dropbox.com/s/pufh630xex4t12b/original_female.jpg", meta_keywords: "https://dl.dropbox.com/s/oj2tw4mqyev4jab/female_styled.jpeg")
else
product.send(:update_attributes!, name: "casual male style")
product.send(:update_attributes!, meta_description: "https://dl.dropbox.com/s/6639119teyh533i/original_male.jpeg", meta_keywords: "https://dl.dropbox.com/s/376mmsprv8w1oib/male_style.jpeg")
end
end
@marshallshen
marshallshen / gist:4106990
Created November 18, 2012 19:20
Update the product price
Spree::Product.all.each do |p|
p.update_attributes!(price: p.price+100)
end
@marshallshen
marshallshen / gist:4120753
Created November 20, 2012 20:16 — forked from bclinkinbeard/gist:2698389
D3.js scatterplot in CoffeeScript
dataset = []
w = 500
h = 400
padding = 30
dataset.push [ Math.random() * w, Math.random() * h ] for [0..50]
svg = d3.select("body")
.append("svg")
.attr("width", w)
@marshallshen
marshallshen / deploy_incanter.clj
Created December 12, 2012 22:14
deploy incanter app through Lein
# assume that we have Leiningen installed
mkdir project_name
mkdir project_name/src
# create project.clj
(defproject incanter-helloworld "0.1.0"
:description "The Incanter version of hello world."
:dependencies [[incanter "1.2.3-SNAPSHOT"]]
:main hello)
(use 'incanter.core)
;; basic arithmetic
($= 7 + 8 - 2 * 6 / 2) ; => 9
;; add 5 to each element of the vector [1 2 3]
($= [1 2 3] + 5) ; => (6 7 8)
require 'csv'
module Exporter
DEFAULT_EXPORT_TABLES = [ Invoice, InvoiceItem, Item, Merchant, Transaction, User ]
DESTINATION_FOLDER = "tmp/"
def self.included(klass)
klass.extend ClassLevelMethods
end
@marshallshen
marshallshen / add_index_to_composite_keys.rb
Created May 7, 2013 01:41
Add index on composite keys using Rails migration
def change
create_table :users do |t|
t.references :account #account_id
t.references :profile #profile_id
#...
end
add_index :accounts, [:account_id, :token], :unique => true, :name => 'by_account_token'
add_index :accounts, :profile_id, :unique => true, :name => 'by_profile'
add_index :accounts, [:account_id, :display_name], :name => 'by_account_profile'
@marshallshen
marshallshen / strangeness.rb
Created June 12, 2013 19:18
Strange cases in Ruby due to it's implementation
1.9.3p125 :018 > ""[0..1]
=> ""
1.9.3p125 :019 > ""[0]
=> nil