Skip to content

Instantly share code, notes, and snippets.

View ryancheung's full-sized avatar
🌝

Ryan Cheung ryancheung

🌝
View GitHub Profile
@ryancheung
ryancheung / gist:e16bbdc18165a242cb6e
Created June 22, 2014 17:49
Generate insert sql using ActiveRecord(Rails 4)
record = User.new(username: 'ryan', email: '[email protected]')
record.class.arel_table.create_insert.tap do |im|
im.insert(record.send(:arel_attributes_with_values_for_create, record.attribute_names))
end.to_sql

installing postgresql 9.3.2 on ubuntu 12.04

$ echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list
$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get install postgresql-9.3 postgresql-contrib-9.3

you should succesfully installing postgresql 9.3.2 on your machine.

# rubocop config files for small Rails4/Ruby2 project
# blog post: http://joanswork.com/rubocop-rails-getting-started/
# .rubocop.yml
AllCops:
Include:
- Rakefile
- config.ru
Exclude:
# replace file basename with a UUID when uploaded
# e.g.: avatar.jpg becomes 41bf830f-80cf-4efe-ad90-3b29bc6708b5.jpg
# but don't regenerate a UUID each time file url is accessed
class User < ActiveRecord::Base
has_attached_file :avatar, path: ":basename.:extension"
before_validation { set_avatar_file_name }
def set_avatar_file_name
# replace any NEW filename with a UUID
#! /bin/bash
### BEGIN INIT INFO
# Provides: redis-server
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Should-Start: $local_fs
# Should-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: redis-server - Persistent key-value db
@ryancheung
ryancheung / randutils.hpp
Created July 26, 2016 04:48 — forked from imneme/randutils.hpp
Addresses common issues with C++11 random number generation; makes good seeding easier, and makes using RNGs easy while retaining all the power.
/*
* Random-Number Utilities (randutil)
* Addresses common issues with C++11 random number generation.
* Makes good seeding easier, and makes using RNGs easy while retaining
* all the power.
*
* The MIT License (MIT)
*
* Copyright (c) 2015 Melissa E. O'Neill
*
@ryancheung
ryancheung / vim.txt
Last active August 15, 2016 06:55
VIM replace by regex in all file
:arg **/**.{h,cpp}
:argdo %s/\(new NextAction(.\{-})\)/NextActionPtr(\1)/ge | update
:argdo %s/\(new NextAction([^()]*)\)/NextActionPtr(\1)/ge | update
@ryancheung
ryancheung / inplace_edit_file.rb
Created August 15, 2016 10:43
Inplace edit file with regexp
files = Dir["./**/**.{h,cpp}"]
def inplace_edit(file, bak, &block)
old_stdout = $stdout
argf = ARGF.clone
argf.argv.replace [file]
argf.inplace_mode = bak
argf.each_line do |line|
yield line
#include <iostream>
void bubble_sort(int arr[5]) {
for (int i = 0; i < 5; ++i) {
for (int j = 1; j < (5 - i); ++j) {
if (arr[j-1] > arr[j]) {
int temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
@ryancheung
ryancheung / yardoc_cheatsheet.md
Created January 11, 2017 05:34 — forked from chetan/yardoc_cheatsheet.md
YARD cheatsheet

YARD CHEATSHEET http://yardoc.org

cribbed from http://pastebin.com/xgzeAmBn

Templates to remind you of the options and formatting for the different types of objects you might want to document using YARD.

Modules

Namespace for classes and modules that handle serving documentation over HTTP