Code That Writes Code

6.1 Coding your way to the weekend

6.2 Kernel#eval, Binding#eval

Binding:

Objects of class Binding(类Binding的对象) encapsulate (密封)the execution context at some particular place in the code and retain this context for future use.

如果你想要一段已经执行的代码在今后反复使用,可以用Binding对象封装它。

The variables, methods, value of self, and possibly an iterator block(迭代块) that can be accessed in this context are all retained.

变量,方法,自身的value,甚至迭代块都可以用Binding对象封装储存。

Binding objects can be created using Kernel#binding

eval(string [, filename [,lineno]]) → obj

Evaluates the Ruby expression(s) in string, in the binding's context.

Binding对象的eval方法,可以评估字符串内的Ruby表达式,并返回表达式的值。

If the optional filename and lineno parameters are present, they will be used when reporting syntax errors.

class Myclass
  def my_method
    @x = 1
    binding
  end
end
 
p b = Myclass.new.my_method
p b.eval("@x")
p eval("@x", b)

#结果一样 1

class Anotherclass
  def my_method
    # eval "self", TOPLEVEL_BINDING   #=> main
    eval("xx + yy", TOPLEVEL_BINDING)
  end
end
 
xx = 123
yy = 321
obj = Anotherclass.new
p obj.my_method #=> 444

TOPLEVEL_BINDING: 是预定义常量,表示顶层作用域的Binding对象。

6.24 Strings of Code Vs Blocks

eval只能执行代码字符串,instance_eval和class_eval可以执行代码字符串和block

array = [1,2,3]
x = 'd'
array.instance_eval("self[1] = x")
p array #=> [1, "d", 3]
尽量用block,代码字符串安全性低容易被攻击。同时也难以阅读和修改。

流行的做法是禁止使用eval方法,同时用Dynamic methods和Dynamic Dispatch替代

污染对象

Ruby把外部传入的对象标记为污染对象。Object#taint -> obj.

判断: #tainted? ->true/false

去掉污染 #untaint

谨慎使用安全级别p148页

可以使用proc {}.call 作为洁净室


6.3Hook Method:

Class#inherited是一个实例方法,当一个类被继承时,Ruby会调用这个方法,Class#inherited方法什么也不做,但程序员可以覆写它的行为,像这样的方法称为钩子方法。

inherited(subclass):Callback invoked whenever a subclass of the current class is created.

更多的钩子方法:

Module#included,#prepended,#method_added, #method_removed, #method_undefined (只对实例方法有用),#singleton_method_added...

这种钩子方法写在模块里,用类方法的方式定义:

module M1
  def self.included(othermod)
    puts "m1 was included into #{othermod}"
  end
end
 
class C
  include M1
end
#=>m1 was included into C
 

另外一种方式钩住同一个事件,:#include方法可以覆写 ,但注意写在类C中。

我的理解类C.调用include方法,就是self.include。先是在自身调用覆写的include方法,然后用super关键字调用原始方法。

⚠️ 类方法是不能被继承的只能自己用。

⚠️ 类包含模块后默认是获得实例方法。除非用#extend

module M
  def hello
    puts "world"
  end
end
 
class C
  def self.include(mod)
    puts "Called: C.include(#{mod})"
    super  #因为覆写了include,所以需要用super调用原始的include功能,否则M不会被C包含
  end 
  include(M)
end
 
C.new.hello✅

类方法和钩子方法结合的技巧:P160页。

最新文章

  1. vim 中替换
  2. HBase之计数器
  3. iOS开发之应用内检测手机锁屏,解锁状态
  4. Sharepoint 2010 创建栏 计算栏
  5. cocosbuilder中的Callbacks和sound effects
  6. HDOJ 1196 Lowest Bit(二进制相关的简单题)
  7. 在linux中限制用户ftp访问权限
  8. 复习-C语言内嵌汇编-初级(2)
  9. How to access the properties of an object in Javascript
  10. python之SQLAlchemy ORM 上
  11. centos下搭建多项目svn服务器
  12. Spring基础知识之基于注解的AOP
  13. 虚拟机克隆,并设置新的ip
  14. Android Studio 调试快捷键
  15. 24.QTableView函数使用,右击菜单实现
  16. sublime插件不能使用,提示plugin_host has exited unexpectedly
  17. 路由网关---zuul
  18. 进程间通信之信号量、消息队列、共享内存(system v的shm和mmap)+信号signal
  19. python ros 重新设置机器人的位置
  20. Linux日常积累

热门文章

  1. 去除input的默认样式
  2. JsonPath的使用
  3. Java性能优化——HashCode的使用
  4. 为什么学Python语言,只需四步全面了解Python语言
  5. COOKIE与SESSION、Django的用户认证、From表单
  6. Python3 Selenium定位不到元素常见原因及解决办法
  7. 20165310_Exp2实验三《敏捷开发与XP实践》
  8. 自己写操作系统---bootsector篇
  9. 硬盘分区表知识—详解硬盘MBR
  10. 格子中输出|2015年蓝桥杯B组题解析第四题-fishers