Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 4. Programming Ruby > Metaprogramming

4.7. Metaprogramming

Metaprogramming is defined as the act of programming code that is able to manipulate itself (or other code). A practical example of this showed up a few sections ago when you used methods such as attr_accessor to dynamically obtain getter and setter methods for instance variables. The ability to reopen classes, define classes and methods conditionally, and call methods that get defined when first invoked, are all good examples of Ruby's metaprogramming abilities.

Metaprogramming is generally encouraged within the Ruby community partially because it's particularly useful when defining Domain Specific Languages (DSLs). Most Ruby developers would agree that powerful metaprogramming techniques should not be abused when writing code that may end up becoming "too clever for its own good," but that a decent mastery of the subject can really separate a beginner from a pro Ruby programmer.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint