Skip to content

Instantly share code, notes, and snippets.

View bachue's full-sized avatar

Bachue Zhou bachue

View GitHub Profile
@bachue
bachue / git.bachue.brewupdate.plist
Created April 16, 2014 14:20
For Mac OS X, run `brew update` every hour. Copy this file to ~/Library/LaunchAgents/git.bachue.brewupdate.plist to install.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>git.bachue.brewupdate</string>
<key>ProcessType</key>
<string>Background</string>
<key>EnvironmentVariables</key>
<dict>
def f
puts 'f'
{}
end
def g
puts 'g'
1
end
@bachue
bachue / strange_proc.rb
Created October 12, 2013 14:37
Why there's no `yield` in Proc.new but Proc.new can accept the block of the caller?
def f
Module.new &Proc.new
end
M = f do
def self.x
'hello world'
end
end
@bachue
bachue / datalist_test.html
Created October 9, 2013 05:34
an example about dynamic datalist, work for chrome & firefox
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails: Welcome aboard</title>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(function() {
$('#input').on('input', function(e) {
var val = $(this).val();
if(val === "") return;
@bachue
bachue / join_table_name.rb
Created September 29, 2013 06:07
原来ActiveRecord默认约定的join表名字是两个表哪个名字短哪个就排在前面啊 以前一直不知道的说
# Generates a join table name from two provided table names.
# The names in the join table names end up in lexicographic order.
#
# join_table_name("members", "clubs") # => "clubs_members"
# join_table_name("members", "special_clubs") # => "members_special_clubs"
def join_table_name(first_table_name, second_table_name)
if first_table_name < second_table_name
join_table = "#{first_table_name}_#{second_table_name}"
else
join_table = "#{second_table_name}_#{first_table_name}"
@bachue
bachue / test_assoc.rb
Created September 28, 2013 03:35
ActiveRecord 测试复杂的关系
class User < ActiveRecord::Base
has_and_belongs_to_many :followees, class_name: 'User', foreign_key: 'follower_id', association_foreign_key: 'follow_id', :join_table => 'follows'
has_and_belongs_to_many :followers, class_name: 'User', foreign_key: 'follow_id', association_foreign_key: 'follower_id', :join_table => 'follows'
has_many :followers_comments, through: :followers, :source => :comments
has_many :comments
end
http://192.168.41.135:8080/solr/admin/cores?action=CREATE&name=collection2&instanceDir=/usr/local/collection2&config=/usr/local/collection2/conf/solrconfig.xml&schema=/usr/local/collection2/conf/schema.xml&dataDir=/usr/local/collection2/data
@bachue
bachue / pry_debug_info
Last active December 19, 2015 11:38
When I visit the file system by ruby httpd, it will crash when some Chinese file names in the folder. I debugged it here, the encoding of regular expression is ascii, but the Chinese file names encoding is UTF-8, so it could be the root cause.
From: /Users/zhour6/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httputils.rb @ line 444 WEBrick::HTTPUtils._escape:
440: def _escape(str, regex)
441: begin
442: str.gsub(regex){ "%%%02X" % $1.ord }
443: rescue Exception
=> 444: require 'pry'; binding.pry
445: end
446: end
@bachue
bachue / Errno in Ruby
Created May 26, 2013 06:59
Displayed by `Errno.constants.each {|errno| err = Errno.const_get(errno); puts "#{err}: #{err.new.message}" }`
Errno::ENOSYS: Function not implemented
Errno::EDESTADDRREQ: Destination address required
Errno::ESHUTDOWN: Can't send after socket shutdown
Errno::ENODEV: Operation not supported by device
Errno::ENOMEM: Cannot allocate memory
Errno::EMLINK: Too many links
Errno::EPROTO: Protocol error
Errno::ENETUNREACH: Network is unreachable
Errno::EIO: Input/output error
Errno::EMFILE: Too many open files