Skip to content

Instantly share code, notes, and snippets.

@xiejiangzhi
xiejiangzhi / gist:51946fe2c8a44643bf51945789e1d032
Last active September 21, 2016 09:48
ruby_server_init.sh
printf "Input ruby version, example '2.2.3': "
read ruby_version
printf "RVM Use taobao source? (y/N): "
read use_taobao_source
if [[ $use_taobao_source == "n" || $use_taobao_source == "N" ]]; then
use_taobao_source=""
fi
if [[ $(which rvm) ]]; then
#!/usr/bin/env ruby
# Referece http://blog.skylight.io/hunting-for-leaks-in-ruby/
require 'set'
require 'oj'
require 'shellwords'
if ARGV.length != 3
puts "Usage: detect_leaks [FIRST.json] [SECOND.json] [THIRD.json]"
# Daemon.new.run do
# `echo "#{Process.pid} #{Time.now}" >> /tmp/daemon_ruby`
# end
require 'timeout'
require 'socket'
require 'logger'
require 'fileutils'
@xiejiangzhi
xiejiangzhi / config.ru
Created March 29, 2016 03:08
ruby rack app
app = Proc.new do |env|
['200', {'Content-Type' => 'text/html'}, ['Hello, this is a rack app.']]
end
run app
@xiejiangzhi
xiejiangzhi / web.rb
Last active March 29, 2016 03:09
ruby test web
require 'rack'
app = Proc.new do |env|
['200', {'Content-Type' => 'text/html'}, ['Hello, this is a rack app.']]
end
port = ENV['PORT'] || 8080
Rack::Handler::WEBrick.run app, Port: port
db_config = YAML.load_file('config/my_database.yml')
resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(db_config)
spec = resolver.spec(env)
conn_pool = ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec)
@xiejiangzhi
xiejiangzhi / autoload_settings
Created October 8, 2015 06:14
auto load settings
class Settings
class << self
Dir[Rails.root.join('config/settings/*')].each do |filepath|
filename = File.basename filepath
next unless filename =~ /^\w+\.ya?ml$/
puts "Load config '#{filename}'"
setting_name = filename.gsub(/\.ya?ml/, '')
eval(%Q{
def #{setting_name}
@xiejiangzhi
xiejiangzhi / sequel_to_active_record
Created October 8, 2015 05:59
migrate Sequel to ActiveRecord
#! /usr/bin/env ruby
require './boot' rescue nil
conn = ActiveRecord::Base.connection
table_name = 'schema_migrations'
cols = conn.execute("show columns from #{table_name}").map {|row| row.first }
unless cols.include?('version')
@xiejiangzhi
xiejiangzhi / cmd_counter_with_ruby
Created August 16, 2015 08:52
统计CLI下输入最多的工具名与输入的命令
#! /usr/bin/env ruby
# Tool top 10
# git 100
# vim 49
# xxx 21
#
# Action top 10
# git checkout master 231
# vim index.html 192
#! /bin/bash
sudo apt-get update
sudo apt-get -y install autoconf automake build-essential libass-dev libfreetype6-dev \
libgpac-dev libsdl1.2-dev libtheora-dev libtool libva-dev libvdpau-dev \
libvorbis-dev libx11-dev libxext-dev libxfixes-dev pkg-config texi2html zlib1g-dev
mkdir ~/ffmpeg_sources
which yasm && echo "yasm has installed" || sudo apt-get -y install yasm
which wget && echo "wget has installed" || sudo apt-get -y install wget