1. Duck Typing
A. 无类型语言
不伤害reliablity 但增加productivity
So even in Java, the class is not always the type—sometimes the type is a subset of the
class, and sometimes objects implement multiple types.
Duck typing: The type of an object is determined by what it can do(behavior), not by its class.
If an object walks like a duck and talks like a duck, then the interpreter is happy to treat it as if it
were a duck.
B. Standard Protocols and Coercions
惯例协议:
1. to_s, to_i
2. to_str, to_int 严格
3. number coercions(coerce)数字的转换由class定,并且可以把参数考虑进来
A class definition is executable code.
scope operator ::
module和class可嵌套定义
2. Classes and bjects
A. 模型
A Ruby object has three components: a set of flags, some instance variables, and an
associated class. A Ruby class is an object of class Class, which contains all the object
things plus a list of methods and a reference to a superclass (which is itself another
class). All method calls in Ruby nominate a receiver (which is by default self, the
current object). Ruby finds the method to invoke by looking at the list of methods in
the receiver’s class.If it doesn’t find the method there, itlooks in any included modules,
then inits super class, modules in the superclass, and then in the super class’s superclass,
and so on.If the method cannot be found in the receiver’s class or any of its ancestors,
Ruby invokes the method method_missing on the original receiver.
virtual class 是针对每个object单建的。
一个object的class如果再度被open,那将不会再新建一个virtual class,只是修改已经建好的这个中的内容。
每include一个module,就会在super链中增加一个proxy class,这个proxy的superintendent指向Object或其他proxy,而proxy的method和instance变量都指向同一个Module中的内容。module不会多建,而proxy会有很多个,并且形成一个链。后加入的插在前面。Proxies位于Class和Object中间。
B. Class and Module Definitions
In Ruby,class and module definitions are executable code.
也就是class和module都是可以动态定义的,和编译好的语言大不相同。
Top-Level Execution Environment 就是Object的实例,所以调用puts等于调用self.puts.
3. Reflection
通过反射发现:
A. 包含的objects
ObjectSpace.each_object() {|x|}
B. class hierarchy
klass.superclass
klass.ancestors 同时还会列出混入的mixin module
C. attributes and methods of object
local_variables
o.instance_variables 执行过的函数后,会有对应的实例变量添加其中
c.class_variables
c.constants
o.methods
o.respond_to?("frozen?")
o.id
o.class
o.kind_of? Fixnum
o.instance_of? Numeric
E. 动态调用method的3种方法
1. o.send(command_string, params)
2. Method object
method = "sss".method(:length)
method.call
UnboundMethod , bind给某个object
3. eval会分析并执行一段合法的ruby代码
trane = %q{"sss".length}
eval trane
性能来说,eval慢许多。
require 'benchmark'
include Benchmark
test = "Stormy Weather"
m = test.method(:length)
n = 100000
bm(1) {|x|
x.report("call") { n.times { m.call } }
x.report("send") { n.times { test.send(:length) } }
x.report("eval") { n.times { eval "test.length" } }
}
F. System hooks 允许改变已存在函数的行为
1. 利用class-open原则,alias_method老的method,重新定义同名的method。
2. callback methods
singleton_method_added 还考虑给某个具体的实例变量增加的method
method_added 针对open一个class时
增加class method时,singleton_method_added 会被通知两次。singleton包含特殊(某个object)的意思。
调用caller得到如何到此的一个call stack(string 组成的 array)
G. YAML for Marshalling
实现to_yaml_properties方法,require yaml.
调用YAML.dump(obj)获取数据,调用YAML.load(data)还原数据。
"compile time” and “runtime.”are all the same. You can add code to a running process.
You can redefine methods on the fly, change their scope from public to private, and
so on. You can even alter basic types, such as Class and Object.
Once you get used to this flexibility, it is hard to go back to a static language such as
C++ or even to a half-static language such as Java.
But then, why would you want to do that?
分享到:
相关推荐
Python学习笔记--皮大庆,非常适合零基础入门。对应英文版本《How to think like a computer scientist》
学习笔记HTML-css-JS.zip学习笔记HTML-css-JS.zip学习笔记HTML-css-JS.zip 学习笔记HTML-css-JS.zip学习笔记HTML-css-JS.zip学习笔记HTML-css-JS.zip 学习笔记HTML-css-JS.zip学习笔记HTML-css-JS.zip学习笔记...
zookeeper云的学习笔记-云的学习笔记系统-云的学习笔记系统源码-云的学习笔记管理系统-云的学习笔记管理系统java代码-云的学习笔记系统设计与实现-基于ssm的云的学习笔记系统-基于Web的云的学习笔记系统设计与实现-...
新版Android开发教程+笔记六--应用3、4 布局 新版Android开发教程+笔记七--基础UI编程1 新版Android开发教程+笔记八--基础UI编程2 新版Android开发教程+笔记九--基础UI编程3 新版Android开发教程+笔记十--基础UI编程...
2004考研通信原理笔记--续大我2004考研通信原理笔记--续大我2004考研通信原理笔记--续大我2004考研通信原理笔记--续大我2004考研通信原理笔记--续大我2004考研通信原理笔记--续大我
信息系统项目管理师考试必过笔记---第十章-项目时间进度管理.docx信息系统项目管理师考试必过笔记---第十章-项目时间进度管理.docx信息系统项目管理师考试必过笔记---第十章-项目时间进度管理.docx信息系统项目管理...
信息系统项目管理师考试必过笔记---第十章-项目时间进度管理.pdf信息系统项目管理师考试必过笔记---第十章-项目时间进度管理.pdf信息系统项目管理师考试必过笔记---第十章-项目时间进度管理.pdf信息系统项目管理师...
LaTex教程--latex排版学习笔记---入门提高.pdf
云的学习笔记-云的学习笔记系统-云的学习笔记系统源码-云的学习笔记管理系统-云的学习笔记管理系统java代码-云的学习笔记系统设计与实现-基于ssm的云的学习笔记系统-基于Web的云的学习笔记系统设计与实现-云的学习...
软考中级 - 软件设计师 - 专题复习笔记3软考中级 - 软件设计师 - 专题复习笔记3软考中级 - 软件设计师 - 专题复习笔记3软考中级 - 软件设计师 - 专题复习笔记3软考中级 - 软件设计师 - 专题复习笔记3软考中级 - 软件...
S7-200 Smart入门笔记3——呼吸灯 S7-200 Smart入门笔记4——全局变量和局部变量 S7-200 Smart入门笔记5——子程序 S7-200 Smart入门笔记6——中断1 S7-200 Smart入门笔记7——中断2 S7-200 Smart入门笔记8——电机...