Skip to content

Instantly share code, notes, and snippets.

View ifyouseewendy's full-sized avatar

Di Wen ifyouseewendy

View GitHub Profile
@ifyouseewendy
ifyouseewendy / pre-push.sh
Last active May 18, 2017 15:26
A hook script to do code checking, linting and testing before git push. Change pre-push.sh to pre-push before use.
#!/bin/bash
# http://misc.flogisoft.com/bash/tip_colors_and_formatting
color_echo () {
echo -e "\033[1;97;44m==> $1 \033[m"
}
color_echo "yarn lint"
yarn lint
@ifyouseewendy
ifyouseewendy / use-sprockets-ouside-rack-application.rake
Created March 28, 2017 21:42
Use Sprockets outside Rack application. I'm working on a script project which is non Rack based. I want to send users an email notification when the script detects a state change. To use Sprockets outside a Rack application is not a common case, so there are not many articles talking about it online. You could count on this.
desc 'Compile scss files using Sprockets
+ source file: #{root}/lib/assets/stylesheets/application.scss,
in which you could require the third party assets
+ output file: #{root}/build/emai.css
'
task :compile do
require 'third_party_assets'
root = '~/Workspace/project/'
@ifyouseewendy
ifyouseewendy / index.html
Created March 19, 2017 16:03
Tic Tac Toe
<div id="errors" style="
background: #c00;
color: #fff;
display: none;
margin: -20px -20px 20px;
padding: 20px;
white-space: pre-wrap;
"></div>
<div id="container"></div>
<script>
# Use a max heap to sort an array
def heap_sort!(array) # Use ! to represent inplace
# setup
n = array.length
array.unshift nil
# build
(n/2).downto(1) do |k|
sink(array, k, n)
end
port module Main exposing (..)
import Html exposing (..)
import Html.Events exposing (..)
main =
program
{ init = init
, view = view
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Html.App as Html
-- model
type alias Model =
{
shelves : Int,
@ifyouseewendy
ifyouseewendy / redis_client_ip.rb
Last active May 25, 2016 10:16
Get client ips of redis connection.
#!/usr/bin/env ruby
# Usage
#
# echo 'client list' | redis-cli -h $HOST -p $PORT | ./redis_client_ip.rb
#
# Reference
#
# http://www.jstorimer.com/blogs/workingwithcode/7766125-writing-ruby-scripts-that-respect-pipelines
# http://redis.io/commands/client-list
@ifyouseewendy
ifyouseewendy / flatten_a_nesting_array.rb
Created March 14, 2016 07:17
Flatten an array of arbitrarily nested arrays of integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4].
# In Ruby, we can just use Array#flatten
#
# def flatten(array)
# array.flatten
# end
# In case you want a manmade version
def flatten(array)
array.reduce([]) do |res, element|
Array === element ? res += flatten(element) : res << element
@ifyouseewendy
ifyouseewendy / pg.rake
Last active January 4, 2016 14:56 — forked from hopsoft/db.rake
Rails rake tasks for dump & restore of PostgreSQL databases
# lib/tasks/pg.rake
namespace :pg do
desc "Dumps the database to backups"
task :dump => :environment do
cmd = nil
with_config do |app, host, db, user|
cmd = "pg_dump -h #{host} -d #{db} -U #{user} -Ft -v -c -f #{Rails.root}/public/resources/duoduo/db_backup/#{Time.now.strftime("%Y%m%d%H%M%S")}_#{db}.tar"
end
puts cmd
exec cmd
require 'thread'
QUEUE_SIZE = 2
POOL_SIZE = 5
queue = SizedQueue.new(QUEUE_SIZE)
producers = POOL_SIZE.times.map do
Thread.new do
item = rand(10)