CSC/ECE 517 Spring 2013/ch1b 1k hf: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
(→‎Features in Ruby that Enable Metaprogramming: added content to about the three features of Ruby that enable metaprogramming)
Line 35: Line 35:
== Features in Ruby that Enable Metaprogramming ==
== Features in Ruby that Enable Metaprogramming ==


*Dynamic?
*Ruby is a [http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing dynamically typed language]
*Interpreted
*Ruby is an [http://en.wikipedia.org/wiki/Interpreted_language interpreted language]
*Open
*Ruby classes are [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open]
 
::In dynamically typed languages, the majority of its type checking is performed at run-time, as opposed to compile time.<ref>[http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing Wikipedia Article on the Type System]</ref>  Because variables are not type-checked until they are used in program execution, it is possible to use types that are created during program execution.
 
::Interpreted languages avoid explicit compilation<ref>[http://en.wikipedia.org/wiki/Interpreted_language Wikipeida Article on Interpreted Language]</ref>.  Because Ruby is being interpreted as it is being executed, it is possible to create new classes and methods during program execution.
 
::Most object-oriented programming languages adhere to the [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open/closed principle]<ref>[http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle Wikipedia Article on the Open/Closed Principle]</ref>.  This means that the core components that are part of the language cannot be modified.  In Ruby, the classes are open, meaning that all of the classes and methods be changed.


== Examples of Metaprogramming in Ruby ==
== Examples of Metaprogramming in Ruby ==

Revision as of 15:50, 20 February 2013

  • Here is a link to the writing assignment for this topic.
  • Here is a link to previous work on this topic.


A Basic Definition of Metaprogramming

The most basic definition of metaprogramming is: writing code that writes or manipulates code. <ref>Video Lecture on Metaprogramming by Dr. Gehringer</ref> <ref name="wiki metaprogramming">Wikipedia Article on Metaprogramming</ref>

A metaprogram can either manipulate itself, or it can manipulate some other program.

Below are three important terms associated with a metaprogramming<ref name="wiki metaprogramming">Wikipedia Article on Metaprogramming</ref>:
  • metalanguage - the language in which a metaprogram is written
  • object language - the language of the program that is being manipulated by a metaprogram
  • reflection - the ability of a programming language to be its own metalanguage (also known as reflexivity)


Uses of Metaprogramming

Metaprogramming can allow programmers to write code that is more concise and more flexible. It can be more concise because a program that can write code is able to generate more code than it is initially given. It can be more flexible, because it can allow the program to change the way it behaves without having to recompile.

One of the best examples of metaprogramming is that of the compiler<ref>Overview of Metaprogramming</ref>. A compiler uses code written in one language in order to generate code that can be executed by a computer.
Metaprogramming is also used for building frameworks such as Ruby on Rails. In this case, the use of convention over configuration allows developers to write more concise and consistent code. Following specific conventions, developers write a relatively small amount of code. The Ruby on Rails scaffold uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized.
One of the most powerful uses for metaprogramming, is the ability to change a program during runtime. In languages such as Ruby and Groovy, programmers can<ref>Coding Insights Blog</ref>:
  • Add methods to objects at runtime
  • Determine if an oject responds to a message or contains a property
  • Respond to calls made on non-existent methods
  • Respond to queries made on non-existent properties


Features in Ruby that Enable Metaprogramming

In dynamically typed languages, the majority of its type checking is performed at run-time, as opposed to compile time.<ref>Wikipedia Article on the Type System</ref> Because variables are not type-checked until they are used in program execution, it is possible to use types that are created during program execution.
Interpreted languages avoid explicit compilation<ref>Wikipeida Article on Interpreted Language</ref>. Because Ruby is being interpreted as it is being executed, it is possible to create new classes and methods during program execution.
Most object-oriented programming languages adhere to the open/closed principle<ref>Wikipedia Article on the Open/Closed Principle</ref>. This means that the core components that are part of the language cannot be modified. In Ruby, the classes are open, meaning that all of the classes and methods be changed.

Examples of Metaprogramming in Ruby

Metaprogramming in Other Languages

The C preprocessor (CPP)

First let's look at metaprogramming that involves textual macro languages. Textual macros are macros that directly affect the text of the programming language without knowing about or dealing with the meaning of the language.

The SWAP macro:
  #define SWAP(a, b, type) { 
      type __tmp_c; c = b; b = a; a = c; 
  }

This macro allows you to swap the two values of the given type. A macro is effective in this situation because:

  • A function call would take too much overhead for a simple operation
  • The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers.
  • A different function would need to be coded for each type of item you wanted to swap.


 #define SWAP(a, b, type) { type __tmp_c; c = b; b = a; a = c; }
 int main()
 {
     int a = 3;
     int b = 5;
     printf("a is %d and b is %d\n", a, b);
     SWAP(a, b, int);
     printf("a is now %d and b is now %d\n", a, b);
     return 0;
 }
The MIN macro:
 #define MIN(x, y) ((x) > (y) ? (y) : (x))

This macro returns the the minimum of two values.

References

<references/>