Skip to content

leanhdaovn/rubymetaprogramming

Repository files navigation

Ruby Metaprogramming

Intro to metaprogramming in Ruby

Ruby Metaprogramming

0. Metaprogramming

  • [Wiki] Metaprogramming is the writing of computer programs with the ability to treat programs as their data. It means that a program could be designed to read, generate, analyze or transform other programs, and even modify itself while running.

1. Objects and Classes

2. self

  • a special variable that always references the current object
  • the default receiver of method calls

3. Modules and Mixins

  • include - By default, it mixes in the specified module's methods as instance methods in the target module/class. e.g.
  • extend - adds the specified module's methods and constants to the target's metaclass (i.e. the singleton class) e.g.
  • included
  • extended

4. Method calling

  • a.dosomething
  • a.send(:do_something)
  • a.method(:do_something).call

5. Metaclass / Singleton Class

6. Methods for metaprogramming

  • define_method
  • method_missing
  • eval / instance_eval / class_eval / module_eval
  • class_variable_get / class_variable_set / instance_variable_get / instance_variable_set / const_get / const_set

7. More readings

Author: leanhdaovn

About

Intro to metaprogramming in Ruby

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages