This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'aquarium' | |
include Aquarium::Aspects | |
class A | |
def f | |
puts 'A#f' | |
end | |
def g | |
puts 'A#g' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'aquarium' | |
include Aquarium::Aspects | |
class A | |
def f | |
puts 'A#f' | |
end | |
def g | |
puts 'A#g' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
再来说说有三个等号的比较操作===。 | |
通常情况下这中方式与==是一样的,但是在某些特定情况下,===有特殊的含义: | |
在Range中===用于判断等号右边的对象是否包含于等号左边的Range; | |
正则表达式中用于判断一个字符串是否匹配模式, | |
Class定义===来判断一个对象是否为类的实例, | |
Symbol定义===来判断等号两边的符号对象是否相同。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# main file | |
def f1 | |
require '2.rb' | |
end | |
def f2 | |
require '2.rb' | |
end | |
f1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def f | |
class C | |
def g | |
'hello world' | |
end | |
end | |
puts C.new.g | |
end | |
f # => SyntaxError, class definition in method body |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// main file | |
def f | |
require '2' | |
puts C.new.g | |
end | |
f # => 'hello world' | |
// 2.rb | |
class C |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def a(&block) | |
puts 'enter a' | |
b &block | |
puts 'exit a' | |
end | |
def b(&block) | |
puts 'enter b' | |
c &block | |
puts 'exit b' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
OlderNewer