Skip to content

Instantly share code, notes, and snippets.

View bachue's full-sized avatar

Bachue Zhou bachue

View GitHub Profile
@bachue
bachue / gembundler_bug_report
Created October 15, 2012 15:33
GemBundler Bug Report
1. bundle gem json1 # create a gem
2. cd json1
3. modify json1.gemspec to this:
# -*- encoding: utf-8 -*-
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'json1/version'
Gem::Specification.new do |gem|
gem.name = "json1"
@bachue
bachue / gist:4245021
Created December 9, 2012 14:03
Aquarium Hello world
require 'aquarium'
include Aquarium::Aspects
class A
def f
puts 'A#f'
end
def g
puts 'A#g'
@bachue
bachue / gist:4245070
Created December 9, 2012 14:11
Aquarium can't deal with new methods
require 'aquarium'
include Aquarium::Aspects
class A
def f
puts 'A#f'
end
def g
puts 'A#g'
@bachue
bachue / Ruby的===
Last active December 9, 2015 22:08
再来说说有三个等号的比较操作===。
通常情况下这中方式与==是一样的,但是在某些特定情况下,===有特殊的含义:
在Range中===用于判断等号右边的对象是否包含于等号左边的Range;
正则表达式中用于判断一个字符串是否匹配模式,
Class定义===来判断一个对象是否为类的实例,
Symbol定义===来判断等号两边的符号对象是否相同。
class A
def self.name; 'A'; end
def self.f; "#{name}#{superclass.f if superclass.respond_to? :f}"; end
end
class B < A
def self.name; 'B'; end
end
class C < B
# main file
def f1
require '2.rb'
end
def f2
require '2.rb'
end
f1
@bachue
bachue / gist:4431884
Last active December 10, 2015 12:08
You can't include a class description in a method, it causes SyntaxError. But you can require a file with a class description in a method, please check https://gist.github.com/4431893
def f
class C
def g
'hello world'
end
end
puts C.new.g
end
f # => SyntaxError, class definition in method body
@bachue
bachue / gist:4431893
Last active December 10, 2015 12:08
You can require a file with class description in a method, it works. But you can't include a class description directly in a method, it will cause SyntaxError, please check https://gist.github.com/4431884
// main file
def f
require '2'
puts C.new.g
end
f # => 'hello world'
// 2.rb
class C
@bachue
bachue / gist:4444172
Created January 3, 2013 15:11
`break` in block 原来`break`拥有跳出所有block的能力
def a(&block)
puts 'enter a'
b &block
puts 'exit a'
end
def b(&block)
puts 'enter b'
c &block
puts 'exit b'
@bachue
bachue / gist:4472731
Last active December 10, 2015 18:08
Finish a simple method alias, very easy implementation. super is also available in the block, it will call the same-name-method in superclass
class A
def f() 'This is OldA#f' end
end
puts A.new.f # => This is OldA#f
# ----------------
def alias_class_chain(cls, property, &block)
old_class_name = "#{cls.name}Without#{property}"