Skip to content

huangcd/MetaProgrammingRuby

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MetaProgramming Ruby Notes

Author: huangcd


对象模型

Open Class(打开类)

打开类大概是Ruby里面最简单也是最通用的MetaProgramming技巧了。 通过打开类的技巧,可以动态地给Ruby类或者实例增加方法和属性。

打开类方法是重新定义一个类,然后在里面添加方法或者属性。

方法

Ruby中的方法包括类方法(如new)和实例方法两种。与Java、C#等不同的是,类的实例不能调用类的方法。

Ruby支持在运行时动态增加类方法和实例方法。

定义类方法有四种方式:

  1. 在类的内部通过def self.method_name; end定义
  2. 在类的内部使用class << self; def method_name; end; end定义
  3. 在外部使用def Object.method_name; end定义
  4. 2、3的混合

About

notes and code for MetaProgrammng Ruby

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages