Skip to content

Instantly share code, notes, and snippets.

@jhjguxin
jhjguxin / replace_words.rb
Created July 3, 2013 10:09
Read, edit, and write a text file line-wise using Ruby
# http://stackoverflow.com/questions/4397412/read-edit-and-write-a-text-file-line-wise-using-ruby
# files = Dir.glob(File.dirname(__FILE__)+"/../app/**/*.rb")
files = Dir[Rails.root.join("app", "**/*.rb")]
files.each do |f_name|
# puts f_name
File.open(f_name, "r+") do |f|
old_pos = 0
f.each do |line|
@jhjguxin
jhjguxin / mongodb_replication_master.markdown
Last active March 14, 2022 08:19
how switch master/slave between replSet model on mongodb some prepare for migrate database from grandcloud to aliyun clound
@jhjguxin
jhjguxin / rails3_with_dalli.md
Created November 19, 2013 07:24
Config Rails 3 with dalli(memcahed)

Config Rails 3 with dalli(memcahed)

To install dalli in your Rails 3 app, simply add the following to your Gemfile and run bundle install.

# Gemfile
gem "dalli", "~> 2.6.4",   :platforms => :ruby

To setup Rails to use the Dalli client for production, add the following to config/environments/production.rb:

@jhjguxin
jhjguxin / date_time.js
Created January 10, 2013 09:33
js上周(月、季)、上上周(月、季)、去年同期上周(月、季)
/***
* 获得当前时间
*/
function getCurrentDate(){
return new Date();
};
//今年以来的起止时间段
function getThisYear(){
//起止日期数组
@jhjguxin
jhjguxin / Rakefile
Created June 20, 2012 05:26
how to resque
#No such file to load -- devise/confirmations_controller
#resque worker devise not eager loading
require File.expand_path('../config/application', __FILE__)
require 'rake/dsl_definition'
require 'resque/tasks'
Askjane::Application.load_tasks
task "resque:setup" => :environment do
@jhjguxin
jhjguxin / generate_source_tracker.rb
Last active October 1, 2020 18:58
delegate demo, use helpers and url_helper outside controller or helpers ....
## rails runner script/generate_source_tracker_link.rb
#eg: version_number: 20130116-1-1 #data-1(早)/2(晚)-1(article_number)
class GeneraterSourceTracker
#include ActionView::Helpers
#include ActionView::Helpers::UrlHelper
include Rails.application.routes.url_helpers
delegate :url_helpers, to: 'Rails.application.routes'
delegate :helpers, to: 'ActionController::Base'
def tracker_url(source_tracker)
@jhjguxin
jhjguxin / first_vs_limit.markdown
Last active September 10, 2020 08:59
mongoid limit(1).first vs .first vs find_by performance?

.first vs .limit(1) performance?

Q:

Hi there, I noticed the following:

This query:

AvailabilityUpdate.where(hotel_id: "AT_INN_BINDERS").only(:timestamp).limit(1).entries.first.timestamp
@jhjguxin
jhjguxin / git_diff.md
Created August 6, 2012 07:40
Git diff 比较不同版本的差异

Git 比较不同版本文件差异的常用命令格式:

git diff                                       查看尚未暂存的文件更新了哪些部分

git diff filename 查看尚未暂存的某个文件更新了哪些

git diff –cached                    查看已经暂存起来的文件和上次提交的版本之间的差异

git diff –cached filename 查看已经暂存起来的某个文件和上次提交的版本之间的差异
@jhjguxin
jhjguxin / unicorn_tour.md
Last active June 5, 2020 06:00
like unix better,deploy rails application with unicorn + nginx <br/> the tour base on guanxi_cms

Unicorn: Rack HTTP server for fast clients and Unix

Unicorn is an HTTP server for Rack applications designed to only serve fast clients on low-latency, high-bandwidth connections and take advantage of features in Unix/Unix-like kernels. Slow clients should only be served by placing a reverse proxy capable of fully buffering both the the request and response in between Unicorn and slow clients.

Features

  • Designed for Rack, Unix, fast clients, and ease-of-debugging. We cut out everything that is better supported by the operating system, nginx or Rack.
  • Compatible with both Ruby 1.8 and 1.9. Rubinius support is in-progress.
  • Process management: Unicorn will reap and restart workers that die from broken apps. There is no need to manage multiple processes or ports yourself. Unicorn can spawn and manage any number of worker processes you choose to scale to your backend.
  • Load balancing is done entirely by the operating system kernel. Requests never pile up behind a bus