Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4447189
  • 博文数量: 1214
  • 博客积分: 13195
  • 博客等级: 上将
  • 技术积分: 9105
  • 用 户 组: 普通用户
  • 注册时间: 2007-01-19 14:41
个人简介

C++,python,热爱算法和机器学习

文章分类

全部博文(1214)

文章存档

2021年(13)

2020年(49)

2019年(14)

2018年(27)

2017年(69)

2016年(100)

2015年(106)

2014年(240)

2013年(5)

2012年(193)

2011年(155)

2010年(93)

2009年(62)

2008年(51)

2007年(37)

分类: Python/Ruby

2014-12-27 22:56:20

原文地址:Just getting my head around Ruby metaprogramming... the mixin/modules always manage to confuse me.
  • include : mixes in specified module methods as instance methods in the target class
  • extend : mixes in specified module methods as class methods in the target class
So is the major difference just this or is a bigger dragon lurking? e.g.

点击(此处)折叠或打开

  1. module ReusableModule
  2.   def module_method
  3.     puts "Module Method: Hi there!"
  4.   end
  5. end

  6. class ClassThatIncludes
  7.   include ReusableModule
  8. end
  9. class ClassThatExtends
  10.   extend ReusableModule
  11. end

  12. puts "Include"
  13. ClassThatIncludes.new.module_method # "Module Method: Hi there!"
  14. puts "Extend"
  15. ClassThatExtends.module_method # "Module Method: Hi there!"



阅读(1326) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~