http://stackoverflow.com/questions/3512471/non-capturing-group
str = "http://stackoverflow.com/questions/tagged/regex"
str =~ /(http|ftp):\/\/([^\/\r\n]+)(\/[^\r\n]*)?/
puts $1 # => http
# with ?:
| # 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) |
| require 'logger' | |
| # Examples | |
| # | |
| # logger = DistinctFileLogger.new(STDOUT) | |
| # logger.set_error_path('/Users/wendi/tmp/logger/error.log') | |
| # logger.info "processing: 1/10" | |
| # logger.info "processing: 2/10" | |
| # logger.info "processing: 3/10" |
| class MultiLevelPageTable | |
| PAGE_DIRECTORY_BASE_REGISTER = 108 | |
| # Save all intermedial state in binary string | |
| attr_reader :pages, :virtual_address, | |
| :page_directory_index, :page_table_index, :offset, | |
| :page_directory, :page_directory_entry, :page_frame_number, | |
| :page_table, :page_table_entry, :physical_frame_number, | |
| :physical_address, :value |
http://stackoverflow.com/questions/3512471/non-capturing-group
str = "http://stackoverflow.com/questions/tagged/regex"
str =~ /(http|ftp):\/\/([^\/\r\n]+)(\/[^\r\n]*)?/
puts $1 # => http
# with ?:
from http://blog.honeybadger.io/advanced-ruby-hash-techniques/
h = Hash.new { |hash, key| raise ArgumentError.new("No hash key: #{ key }") }
h[:a]=1
h[:a] # 1
h[:x] # raises ArgumentError: No hash key: xcsrf.js
var csrftk = $("meta[name='csrf-token']").attr("content");
// Ajax set
$(document).ajaxSend(function(e, xhr, options) {
xhr.setRequestHeader("X-CSRF-Token", csrftk);
});| # API | |
| # | |
| # + push | |
| # + top | |
| # + pop | |
| # + empty | |
| # + empty? | |
| ArrayStack = Struct.new(:values) do | |
| def push(value) |
There is a weird situation I haven't noticed before:
class Counter
attr_accessor :processed, :processed_names
def initialize
@processed = 0
@processed_names = []
end| git filter-branch -f --commit-filter ' | |
| if [ "$GIT_COMMITTER_NAME" = "<Old name>" ]; | |
| then | |
| GIT_COMMITTER_NAME="<New name>"; | |
| GIT_COMMITTER_EMAIL="<New email>"; | |
| GIT_AUTHOR_NAME="<New name>"; | |
| GIT_AUTHOR_EMAIL="<New email>"; | |
| else | |
| git commit-tree "$@"; | |
| fi' -- --all |