Metaprogramming in Ruby: a pattern catalog

  • Authors:
  • Sebastian Günther;Marco Fischer

  • Affiliations:
  • University of Magdeburg;University of Magdeburg

  • Venue:
  • Proceedings of the 17th Conference on Pattern Languages of Programs
  • Year:
  • 2010

Quantified Score

Hi-index 0.00

Visualization

Abstract

Modern programming languages provide extensive metaprogramming facilities. We understand metaprogramming as the utilization and modification of available language constructs and abstractions while staying inside the language. Metaprogramming requires a rich semantic model of the program that is represented as language constructs. The careful utilization of the constructs allows changing programs and sometimes even the semantics of other language constructs. There are several use-cases for these kinds of extensions: Runtime adaption, program generation, language customization or the design of Domain-Specific Languages (DSL). However, for using metaprogramming effectively, developers need to understand the mechanisms of metaprogramming and need a language that addresses these mechanisms. These are the prerequisites to communicate about metaprogramming and to actively use it in development. This paper's goal is to explore and highlight the metaprogramming facilities of the Ruby programming language. Ruby is an interpreted and fully object-oriented language. Any class and method -- even those of the built-in entities -- can be modified at runtime. We explain the mechanisms in the form of a pattern catalog that is structured along the object's initialization, declaration, composition, and modification. The core contribution is the sensitive grouping of related patterns that exist in literature and to reduce them to the dominant techniques they use. The pattern catalog can be used to communicate about Ruby's metaprogramming capabilities effectively. We explain the patterns with their context, problem, goals, consequences, known uses, and illustrate them in the context of an example application.