Giter Site home page Giter Site logo

metaprogrammingruby's Introduction

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的混合

metaprogrammingruby's People

Contributors

huangcd avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.