CSC/ECE 517 Spring 2013/ch1b 1n jp: Difference between revisions
Line 3: | Line 3: | ||
===Introduction=== | ===Introduction=== | ||
One of the distinguishing features of the Ruby programming | One of the distinguishing features of the Ruby programming language is it's ability to dynamically extend classes to perform a variety of different methods depending upon the needs of the current application. For example, should the developer want to extend a core language class, take String for example, they could easily re-open that class and add new methods dynamically to this existing class, rather than creating an entirely new class. | ||
Even more powerful, Ruby allows for a construct called method_missing(). By overriding this method in your class, method_missing() will attempt to handle any method calls not currently defined by the given class (or class hierarchy). Typically, some sort of regular expression is used to determine if the method call can be redirected to an existing method based on it's naming convention (for example, a method call in a non-existent singular form could be redirected to an existing pluralized form). If the method_missing() definition cannot determine where to redirect the method call, the default is simply to call the parent class via super(). | Even more powerful, Ruby allows for a construct called method_missing(). By overriding this method in your class, method_missing() will attempt to handle any method calls not currently defined by the given class (or class hierarchy). Typically, some sort of regular expression is used to determine if the method call can be redirected to an existing method based on it's naming convention (for example, a method call in a non-existent singular form could be redirected to an existing pluralized form). If the method_missing() definition cannot determine where to redirect the method call, the default is simply to call the parent class via super(). |
Revision as of 14:22, 20 February 2013
Ruby's method_missing(): Advantages/Disadvantages and Alternatives
Introduction
One of the distinguishing features of the Ruby programming language is it's ability to dynamically extend classes to perform a variety of different methods depending upon the needs of the current application. For example, should the developer want to extend a core language class, take String for example, they could easily re-open that class and add new methods dynamically to this existing class, rather than creating an entirely new class.
Even more powerful, Ruby allows for a construct called method_missing(). By overriding this method in your class, method_missing() will attempt to handle any method calls not currently defined by the given class (or class hierarchy). Typically, some sort of regular expression is used to determine if the method call can be redirected to an existing method based on it's naming convention (for example, a method call in a non-existent singular form could be redirected to an existing pluralized form). If the method_missing() definition cannot determine where to redirect the method call, the default is simply to call the parent class via super().
By having the ability to allow a class to dynamically extend itself even if a method definition never truly existed, Ruby truly distinguishes itself from statically typed languages such as Java, where classes will never compile if method calls are not defined at some point in the hierarchy. With method_missing() defined, almost any sort of method call can be handled in the desired manner.
This article will explore the method_missing() construct in greater detail, giving both advantages and disadvantages of having this feature in the Ruby language. We will also examine how other programming languages attempt to handle the same problem.