Skip to content

Instantly share code, notes, and snippets.

View shunwen's full-sized avatar
🐢

shunwen shunwen

🐢
View GitHub Profile
@shunwen
shunwen / is_a.rb
Created July 31, 2013 08:37
Use #is_a? instead of #===
omg = [1,2,3]
omg ===Array # => false
Array === omg # => true
omg.is_a? Array # => true
tmp = 1
tmp.is_a? Array #=> false
@shunwen
shunwen / visit_url.rb
Created June 23, 2013 23:44
How to visit an url in Ruby
# Not suitble for production
require 'open-uri'
open('http://github.com')
require 'net/http'
require 'uri'
uri = URI.parse "http://github.com/"
response = Net::HTTP.get_response uri
# Use system call
@shunwen
shunwen / default_value_in_lambda.rb
Created May 13, 2013 04:48
Use default value in lambda
lambda { |*args| (args.first || default) }
@shunwen
shunwen / test.py
Created December 25, 2012 07:49
Python local variable declared in for loop stays alive outside the loop.
bar = [1,2,3]
for foo in bar:
foo = foo + 1
break
print foo
@shunwen
shunwen / chop_dos_date_time.bat
Created July 23, 2012 07:03
Chop %date% and %time% into useful tokens
REM get date from MM/DD/YYYY
SET YEAR=%DATE:~6,4%
SET MONTH=%DATE:~0,2%
SET DAY=%DATE:~3,2%
SET DATENOW=%YEAR%%MONTH%%DAY%
REM get time from hh:mm:ss.ss
SET hh=%TIME:~0,2%
IF %hh% LSS 10 SET hh=0%hh:~1,1%
SET mm=%TIME:~3,2%
@shunwen
shunwen / worker_skeleton.rb
Created January 13, 2015 07:50
Worker skeleton
class TestWorker
def startup
# Change process name
$PROGRAM_NAME = "HL7 - noop working..."
# Create pid file
File.open("tmp/pids/workers.pid", "a") {|f| f.puts Process.pid }
# Go daemon and disconnect from STDOUT/STDERR
@shunwen
shunwen / .kitchen.yml
Created December 2, 2014 03:21
chef apache cookbook test
---
driver:
name: docker
socket: <%= ENV['DOCKER_HOST'] %>
provisioner:
name: chef_zero
platforms:
- name: ubuntu-14.04
@shunwen
shunwen / rename_files.sh
Created October 29, 2014 08:36
Rename *.conf to *.conf.erb
for f in *.conf
do
mv ${f}{,.erb}
done
@shunwen
shunwen / Dockerfile
Created May 21, 2014 13:56
Dockerfile Rails
FROM ubuntu:14.04
MAINTAINER Shunwen Hsiao, "https://github.com/shunwen"
RUN apt-get update
RUN apt-get install -qy git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common vim
RUN apt-get clean
# Install nodejs for js runtime
RUN add-apt-repository ppa:chris-lea/node.js
# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.1.1'
require 'active_record'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)