self_vs_default_definee_vs_receiver
最近在学习ruby的过程遇到很多有趣的博客,随记录学习,这篇学习笔记摘自http://yugui.jp/articles/846
#self
ruby中self无处不在,或是显示的调用或是隐含调用,方法调用如果不指明接收者,那么默认也是self。打开pry
ruby --version => ruby 2.4.0p0
p self => main
class Persion
p self => Persion
def hello(param = (p self));end
end
Persion.new.hello => #<Persion:0x007ff03d3b5468>
class Manager
class Employ < (p self; self) => Manager
end
end
#default definee
ruby 中默认存在一个指向 class 的引用,不像 self可以随处调用,这个引用比 self 更加隐含,暂且称之为 default definee, 如果方法定义时不提供默认的接受者,那么方法默认就会作为 default definee 的实例方法,代开pry
def hello;end
Object.instance_method(:hello) => #<UnboundMethod: Object#hello>
class Persion
def hello;end
end
Persion.instance_method(:hello) => #<UnboundMethod: Persion#hello>
在正常的方法体内(def 定义的方法), self 是方法的接受者,但是内部函数的 default deinee 却是外层的class,例如:
class Persion
def hello
def speak;end
end
end
p = Persion.new
p.hello
p.method(:hello) => #<Method: Persion#hello>
Persion.instance_method(:speak) => #<UnboundMethod: Persion#speak>
如果方法内部想要定义实例方法可以用self,本质上方法会被添加到对象的单例类上
class Persion
def hello
def self.speak;end
end
end
p = Persion.new
p.hello
p.method(:hello) => #<Method: Persion#hello>
p.method(:speak) => #<Method: #<Persion:0x007fc5fe1cfe00>.speak>
p.singleton_class.instance_method(:speak) => #<UnboundMethod: #<Class:#<Persion:0x007fc5fe1cfe00>>#speak>
正常方法体内的 default definee 都是外层的class
class Manager;end
$m = Manager.new => #<Manager:0x007f890e474958>
class Employee
def $m.hello(param = (def speak;end))
def sing;end
end
end
$m.hello
Employee.instance_method(:speak) => #<UnboundMethod: Employee#speak>
Employee.instance_method(:sing) => #<UnboundMethod: Employee#sing>
$m.method(:speak) => NameError: undefined method `speak' for class `#<Class:#<Manager:0x007f890e474958>>'
# eval
# instance_eval
instacne_eval 会执行以下操作:
- 修改 self 为 instance_eval 的接受者
- 修改 default definee 为 instance_eval 的单例类
o = Object.new
o.instance_eval do
p self
def hello; end
end
o.method(:hello) => #<Method: #<Object:0x007f890ec8e698>.hello>
o.singleton_class.instance_method(:hello) => #<UnboundMethod: #<Class:#<Object:0x007f890ec8e698>>#hello>
下一个例子
class Persion
$o = Object.new
$o.instance_eval do
def hello(param = (def speak;end))
def sing;end
end
end
end
$o.hello
$o.method(:speak) => #<Method: #<Object:0x007fee5f3f4b98>.speak>
$o.method(:sing) => #<Method: #<Object:0x007fee5f3f4b98>.sing>
Persion.instance_method(:hello) => NameError: undefined method `hello' for class `Persion'
Persion.instance_method(:speak) => NameError: undefined method `speak' for class `Persion'
Persion.instance_method(:sing) =>NameError: undefined method `sing' for class `Persion'
$o.singleton_class.instance_method(:hello) => #<UnboundMethod: #<Class:#<Object:0x007fee5f3f4b98>>#hello>
$o.singleton_class.instance_method(:speak) => #<UnboundMethod: #<Class:#<Object:0x007fee5f3f4b98>>#speak>
$o.singleton_class.instance_method(:sing) => #<UnboundMethod: #<Class:#<Object:0x007fee5f3f4b98>>#sing>
# class_eval
class_eval 会把 self 和 default definee 都修改为class_eval 的接受者
class Persion;end
Persion.class_eval do
p self => Persion
def hello; end
end
Persion.new.method(:hello) => #<Method: Persion#hello>
Persion.instance_method(:hello) => #<UnboundMethod: Persion#hello>
明白了以上几点,那么下面这个例子就很好理解了:
Persion.instance_eval { define_method(:hello) { "hello" } }
Persion.class_eval { define_method(:sing) { "sing" } }
Persion.instance_eval { def speak; 'speak'; end }
Persion.class_eval { def dance; 'dance'; end }
p Persion.new.hello => "hello"
p Persion.new.sing => "sing"
p Persion.speak. => "speak"
p Persion.new.dance => "dance"
self_vs_default_definee_vs_receiver的更多相关文章
随机推荐
- input 标签,不可更改
1.disabled 属性规定应该禁用 input 元素,被禁用的 input 元素,不可编辑,不可复制,不可选择,不能接收焦点,后台也不会接收到传值.设置后文字的颜色会变成灰色.disabled 属 ...
- pandas进行条件格式化以及线性回归的预测
条件格式化 需求1: 将三次考试的成绩小于60分的值找出来,并将字体变为红色 需求2: 将每次考试的第一名找出来,将背景变为绿色 需求3: 使用背景颜色的深浅来表示数值的大小 需求4: 使用数据条的长 ...
- 使用scrapy中xpath选择器的一个坑点
情景如下: 一个网页下有一个ul,这个ur下有125个li标签,每个li标签下有我们想要的 url 字段(每个 url 是唯一的)和 price 字段,我们现在要访问每个li下的url并在生成的请求中 ...
- MTD的认识
MTD即内存计数设备,是linux中对ROM.NORFLASH.NAND Flash等存储设备抽象出来的一个设备层,它向上提供统一的访问接口:读写.擦除等:屏蔽了底层硬件的操作.各类存储设备的差别. ...
- Wordpress 后台更改网址
在 `wp_options` 数据库执行下面两条命令 ```sql update wp_options set option_value = 'your_new_url' where option_n ...
- 51单片机学习笔记(郭天祥版)(6)——键盘的作业题、AD、DA、DS18B20(这里之后看清翔的补一下好了)
A:analog,D:digital AD,就是模拟量转换为数字量,DA就是数字量转换为模拟量 为什么要转换? 单片机是数字芯片,内部只有0和1,没法表示模拟量 比如我们如果需要2.5V怎么办?其实是 ...
- 关于键盘事件对象code值
e.keyCode || e.which || e.charCode; //IE只有keyCode属性,FireFox中有which和charCode属性,Opera中有keyCode和which属性 ...
- 分割list,将集合按规定个数分为n个部分。
/** * 按指定大小,分隔集合,将集合按规定个数分为n个部分 * * @param list * @param len * @return */ public static <T> Li ...
- hbase-多租户
namespace 不同表在不同的namespace,可以做用户的权限控制 资源限制 限制每时间段请求的数量和大小 设置表的空间大小 修改hbase-site.xml文件 添加两个配置 hbase.q ...
- SNORT入侵检测系统
SNORT入侵检测系统 YxWa · 2015/10/09 10:38 0x00 一条简单的规则 alert tcp 202.110.8.1 any -> 122.111.90.8 80 (ms ...