免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2330 | 回复: 1
打印 上一主题 下一主题

Eloquent Ruby 读书笔记 续 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-01 19:30 |只看该作者 |倒序浏览
Eloquent Ruby 读书笔记 续





initializeruby
运算符重载和Ruby风格的运算符重载
程序大概意思是,重新定义,文件类的比较方法。只要目录和文件名一样,就说这两个文件类一样。ruby的
Ruby代码
  1. class DocumentPointer   
  2.   attr_reader :folder, :name  
  3.   def initialize( folder, name )   
  4.     @folder = folder   
  5.     @name = name   
  6.   end  
  7.   def ==(other)   
  8.     return true if other.equal?(self)   
  9.     return false unless other.instance_of?(self.class)   
  10.     folder == other.folder && name == other.name   
  11.   end  
  12. end  

  13. class DocumentPointer
  14.   attr_reader :folder, :name
  15.   def initialize( folder, name )
  16.     @folder = folder
  17.     @name = name
  18.   end
  19.   def ==(other)
  20.     return true if other.equal?(self)
  21.     return false unless other.instance_of?(self.class)
  22.     folder == other.folder && name == other.name
  23.   end
  24. end
复制代码
Ruby代码
  1. class DocumentPointer   
  2.   attr_reader :folder, :name  
  3.   def initialize( folder, name )   
  4.     @folder = folder   
  5.     @name = name   
  6.   end  
  7.   def ==(other)   
  8.     return false unless other.respond_to?(:folder)   
  9.     return false unless other.respond_to?(:name)   
  10.     folder == other.folder && name == other.name   
  11.   end  
  12. end  

  13. class DocumentPointer
  14.   attr_reader :folder, :name
  15.   def initialize( folder, name )
  16.     @folder = folder
  17.     @name = name
  18.   end
  19.   def ==(other)
  20.     return false unless other.respond_to?(:folder)
  21.     return false unless other.respond_to?(:name)
  22.     folder == other.folder && name == other.name
  23.   end
  24. end
复制代码
当我们用的时候
Ruby代码
  1. #Asymmetry   
  2. class ContractIdentifier < DocumentIdentifier   
  3. end  
  4.   
  5. doc_id = DocumentIdentifier.new( 'contracts', 'Book Deal' )   
  6. con_id = ContractIdentifier.new( 'contracts', 'Book Deal' )   
  7.   
  8. puts "They are equal!" if doc_id == con_id  

  9. #Asymmetry
  10. class ContractIdentifier < DocumentIdentifier
  11. end

  12. doc_id = DocumentIdentifier.new( 'contracts', 'Book Deal' )
  13. con_id = ContractIdentifier.new( 'contracts', 'Book Deal' )

  14. puts "They are equal!" if doc_id == con_id
复制代码
会出现子类没有父类方法的问题,在contractIdentifier里没有重载操作符。那么

Ruby代码
  1. class VersionedIdentifier < DocumentIdentifier   
  2.   attr_reader :version  
  3.   
  4.   def initialize(folder, name, version)   
  5.     super(folder, name)   
  6.     Well-Behaved Equality 147   
  7.     @version = version   
  8.   end  
  9.   
  10.   def ==(other)   
  11.     if other.instance_of? VersionedIdentifier   
  12.       other.folder == folder &&   
  13.       other.name == name &&   
  14.       other.version == version   
  15.     elsif other.instance_of? DocumentIdentifier   
  16.       other.folder == folder && other.name == name   
  17.     else  
  18.       false  
  19.     end  
  20.   end  
  21. end  
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-03-01 22:36 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP