<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Twfolsom</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Twfolsom"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Twfolsom"/>
	<updated>2026-08-09T00:34:45Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/FINAL_PROJECT_E736_UNDO_DELIVERABLE&amp;diff=75176</id>
		<title>CSC/ECE 517 Spring 2013/FINAL PROJECT E736 UNDO DELIVERABLE</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/FINAL_PROJECT_E736_UNDO_DELIVERABLE&amp;diff=75176"/>
		<updated>2013-04-23T22:05:16Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* Classes Modified */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= E736. Undo Functionality for Expertiza =&lt;br /&gt;
*Contacts: Ed Gehringer (efg@ncsu.edu) and Andrew Kofink (ajkofink@ncsu.edu)&lt;br /&gt;
*[http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2013/FINAL_PROJECT_E736_UNDO Design Document]&lt;br /&gt;
*[https://docs.google.com/a/ncsu.edu/document/d/11YTjxFXFR13vJ769yFBbqn9qOueK2Ktz0Pd-gyS2W5U/ Project Description Doc]&lt;br /&gt;
== Team Members ==&lt;br /&gt;
*Chun Sing Tsui (ctsui@ncsu.edu)&lt;br /&gt;
*Kenny Yarboro (kbyarbor@ncsu.edu)&lt;br /&gt;
*Meng Cao (mcao2@ncsu.edu)&lt;br /&gt;
*Travis Folsom (twfolsom@ncsu.edu)&lt;br /&gt;
&lt;br /&gt;
== Project Deliverables ==&lt;br /&gt;
&lt;br /&gt;
=== Screencast Video Overview ===&lt;br /&gt;
A video overview of the project was created via [http://www.screencast-o-matic.com/ Screencast-O-Matic] and can be found [http://www.youtube.com/watch?v=1DYS5InjBOk here].  &lt;br /&gt;
&lt;br /&gt;
=== Overview of Undo Functionality ===&lt;br /&gt;
*Undo allows the user to undo their most recent actions for any items that offer the undo functionality.  &lt;br /&gt;
*The undo of file submissions was not required; it is not implemented in this project.&lt;br /&gt;
*If an undo is attempted on an item already modified by another user, a message is displayed stating why undo cannot be performed.  Otherwise, the user is able to undo any changes to an item with the undo functionality.  &lt;br /&gt;
*When multiple actions are performed in one step, the undo functionality groups all of the changes together for the purposes of restoring all items affected by the action previously performed.&lt;br /&gt;
&lt;br /&gt;
=== List of Items Now Supported by Undo Functionality === &lt;br /&gt;
*Add Signup Sheet&lt;br /&gt;
**Delete Topic&lt;br /&gt;
**Edit Topic&lt;br /&gt;
**New Topic&lt;br /&gt;
*Assignments&lt;br /&gt;
**Add/Remove Participants&lt;br /&gt;
**Inherit Participants from Course&lt;br /&gt;
**Copy Participants to Course&lt;br /&gt;
**Copy&lt;br /&gt;
**Create&lt;br /&gt;
**Delete&lt;br /&gt;
**Edit&lt;br /&gt;
*Courses&lt;br /&gt;
**Add Participants&lt;br /&gt;
**Copy&lt;br /&gt;
**Create&lt;br /&gt;
**Delete&lt;br /&gt;
**Edit&lt;br /&gt;
**Toggle Private/Public&lt;br /&gt;
*Import&lt;br /&gt;
**Import Participants&lt;br /&gt;
**Import Teams&lt;br /&gt;
**Import Users&lt;br /&gt;
*Student Actions&lt;br /&gt;
**Create Team&lt;br /&gt;
**Edit Team Name&lt;br /&gt;
**Leave Team&lt;br /&gt;
**Submit Hyperlink&lt;br /&gt;
*Questions&lt;br /&gt;
**Copy&lt;br /&gt;
**Create&lt;br /&gt;
**Delete&lt;br /&gt;
**Edit&lt;br /&gt;
*Questionnaires&lt;br /&gt;
**Copy&lt;br /&gt;
**Create&lt;br /&gt;
**Delete&lt;br /&gt;
**Edit&lt;br /&gt;
*Teams&lt;br /&gt;
**Add&lt;br /&gt;
**Create (Manual)&lt;br /&gt;
**Create (Random)&lt;br /&gt;
**Delete&lt;br /&gt;
**Delete All Teams&lt;br /&gt;
**Edit&lt;br /&gt;
&lt;br /&gt;
=== Redo Functionality ===&lt;br /&gt;
In addition to the Undo functionality being added, the ability to Redo an undone action is also implemented.  Whenever an Undo is performed, the user is given an option to Redo the action that was undone.  Whenever a Redo is performed, the user is given an option to Undo the action again.  &lt;br /&gt;
&lt;br /&gt;
=== Design Patterns and Designs ===&lt;br /&gt;
*DRY&lt;br /&gt;
**&amp;quot;Don't Repeat Yourself&amp;quot; practice was followed. &lt;br /&gt;
**The app/controllers/application_controller.rb file contains all of the Undo functionality code for Expertiza.  All other controllers are able to call the necessary undo-related methods to enable the Undo functionality.&lt;br /&gt;
&lt;br /&gt;
=== Framework Leveraged &amp;amp; Enhanced ===&lt;br /&gt;
*Paper Trail(https://github.com/airblade/paper_trail)&lt;br /&gt;
**Paper Trail allows for tracking changes to model data. &lt;br /&gt;
***Good for auditing or versioning. &lt;br /&gt;
**Offers many functionalities including:&lt;br /&gt;
****Viewing a model at any stage in its lifecycle. &lt;br /&gt;
****Reverting a model to any version.&lt;br /&gt;
****Undeleting items after they have been destroyed.&lt;br /&gt;
**Compatible with Rails-2.3.15, which is the version Expertiza uses. &lt;br /&gt;
**There is a [http://railscasts.com/episodes/255-undo-with-paper-trail Railscast] showing how to implement the undo feature with Paper Trail. &lt;br /&gt;
**Ranked as the most popular framework for Active Record versioning feature in [https://www.ruby-toolbox.com/categories/Active_Record_Versioning here]&lt;br /&gt;
*Enhancement&lt;br /&gt;
**Paper Trail only supported undo of the most recent action and did not restore associations.   &lt;br /&gt;
**Paper Trail customized to add support for restoring associations within the Expertiza system.  &lt;br /&gt;
***Database transactions paired with timestamps and user ids to group them together.&lt;br /&gt;
&lt;br /&gt;
=== Classes Modified ===&lt;br /&gt;
*apps/&lt;br /&gt;
**controllers/&lt;br /&gt;
***application_controller.rb&lt;br /&gt;
***assignment_controller.rb&lt;br /&gt;
***course_controller.rb&lt;br /&gt;
***import_file_controller.rb&lt;br /&gt;
***participants_controller.rb&lt;br /&gt;
***sign_up_sheet_controller.rb&lt;br /&gt;
***student_team_controller.rb&lt;br /&gt;
***team_controller.rb&lt;br /&gt;
***teams_users_controller.rb&lt;br /&gt;
***versions_controller.rb&lt;br /&gt;
**models/&lt;br /&gt;
***assignment_participant.rb&lt;br /&gt;
***assignment.rb&lt;br /&gt;
***course.rb&lt;br /&gt;
***participant.rb&lt;br /&gt;
***sign_up_topic.rb&lt;br /&gt;
***ta_mapping.rb&lt;br /&gt;
***team_node.rb&lt;br /&gt;
***teams_user.rb&lt;br /&gt;
&lt;br /&gt;
=== Unit &amp;amp; Functional Tests ===&lt;br /&gt;
The implementation of the Undo functionality did not afford any testing opportunities applicable to unit or functional tests.&lt;br /&gt;
&lt;br /&gt;
=== Bug Fixes &amp;amp; Other Improvements ===&lt;br /&gt;
The following bugs were found within Expertiza and fixed:&lt;br /&gt;
*ApplicationController enhanced to rescue from ActiveRecord::RecordNotFound errors and redirect to tree display.  &lt;br /&gt;
*Copy of Assignment opened Edit page for the original Assignment and now correctly opens the Edit page for the copy of the assignment.  &lt;br /&gt;
&lt;br /&gt;
The following general improvements were made:&lt;br /&gt;
*Explicitly declared the has_one association between each object and its tree node to eliminate the manual deletion of a related tree node with the object.   &lt;br /&gt;
*Edited the Copy of Course flash message to be more clear that the copy had already been created and to resolve a misspelling.&lt;br /&gt;
&lt;br /&gt;
=== Problem Areas with Expertiza ===&lt;br /&gt;
There were some areas of functionality within Expertiza that did not work.  There were some cases in which the undo functionality could not be added due to the inherent problems.  These problem areas were:&lt;br /&gt;
*Assigning surveys in an assignment.&lt;br /&gt;
*Dates in assignments do not show up when the assignment is edited.   &lt;br /&gt;
*Unable to assign random reviewers. &lt;br /&gt;
*Unable to edit advice for questions in questionnaire. &lt;br /&gt;
*Unable to remove participants from a course.&lt;br /&gt;
*Unable to inherit team from a course&lt;br /&gt;
&lt;br /&gt;
=== Future Recommendations ===&lt;br /&gt;
*Add integration tests. &lt;br /&gt;
*Add undo functionality to other areas of Expertiza (especially student actions such as review responses)&lt;br /&gt;
*Determine appropriate methodology for cleaning up Versions table.&lt;br /&gt;
&lt;br /&gt;
=== Template to Implement Undo Functionality ===&lt;br /&gt;
* Install paper_trail gem. Please follow the steps listed on its [https://github.com/airblade/paper_trail official webpage ]. &lt;br /&gt;
* Add ''has_paper_trail'' to the model you want to have undo functionality&lt;br /&gt;
 class Foo &amp;lt; ActiveRecord::Base&lt;br /&gt;
     has_paper_trail&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
* Call ''undo_link'' method in the corresponding controller. The argument is a string, which you want to show along with the undo link in flash[:note]. The following is code snippet shows how the undo functionality is implemented for the delete method of FoosController&lt;br /&gt;
 class FoosController &amp;lt; ApplicationController&lt;br /&gt;
     def delete&lt;br /&gt;
         @foo = Foo.find(params[:id])&lt;br /&gt;
         @foo.destroy&lt;br /&gt;
         undo_link(&amp;quot;Foo \&amp;quot;#{@foo.name}\&amp;quot; has been deleted successfully. &amp;quot;)&lt;br /&gt;
         redirect_to :back&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74033</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74033"/>
		<updated>2013-03-20T19:45:06Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* List of files changed */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
&lt;br /&gt;
== QuestionnaireController Functional Tests ==&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing QuestionnaireController Test ===&lt;br /&gt;
&lt;br /&gt;
There were a few tests already written for the Questionnaire controller, but they were not passing initially. We had to modify several things in the test and questionnaire fixture in order to get things up and running. The setup method contained login code that was outdated, and thus each test would result in a redirect due to the invalid login credential. So we looked through other more up to date functional test files and found the correct way to login as a particular user. We also put this code in a helper method so we can call it more easily in different tests if we so choose (more on this in the &amp;quot;Helper Methods&amp;quot; section).&lt;br /&gt;
&lt;br /&gt;
In addition, we added a new questionnaire fixture (:questionnaire5) to help with our testing. Through debugging the test error messages, we saw that an additional required column called &amp;quot;section&amp;quot; has been added to the &amp;quot;Questionnaire&amp;quot; model, which was contributing to the failures. We added the additional column accordingly in :questionnaire5.&lt;br /&gt;
&lt;br /&gt;
=== Helper Methods ===&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
* Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
* Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
* Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
=== How to Run Controller Test ===&lt;br /&gt;
&lt;br /&gt;
All of the QuestionnaireController tests are in the questionnaire_controller_test.rb file and can be run as a standard functional test.&lt;br /&gt;
&lt;br /&gt;
== Questionnaire Subclass Unit Tests ==&lt;br /&gt;
&lt;br /&gt;
The original assignment was to write unit tests for the Questionnaire Subclasses. As we reviewed the tests, it was found that most of the methods were trivial (ex: the method &amp;quot;symbol&amp;quot; only only returns a hardcoded symbol).  The only test that was found to be worth testing was the get_weighted_score method.  Upon further review, however, we found that testing this method in each of the four subclasses violated the DRY principle.  Therefore, we moved the test to the superclass (Questionnaire) to test the &amp;quot;get_weighted_score&amp;quot; method.&lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing Questionnaire Subclass Tests ===&lt;br /&gt;
&lt;br /&gt;
There was only one test in the existing unit test for AuthorFeedbackQuestionnaire for testing the &amp;quot;get_weighted_score&amp;quot; method. Even though the test could be run, we found that it was not making assertions as it should. The files for the other 3 questionnaire subclasses were created to complete the testing on them.  As stated earlier, we then found that testing the &amp;quot;get_weighted_score&amp;quot; method was redundant, violating the DRY principle, so it was moved to the superclass (Questionnaire).  The tests in the subclasses were deleted (since they were redundant) but the test files were kept for future testing of the subclass if/when needed.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
Tested the one method &amp;quot;compute_weighted_score&amp;quot; from the subclasses that was not trivial. Based on an average, it was asserted that the value was not nil and that the value was computed correctly.&lt;br /&gt;
&lt;br /&gt;
=== How to Run Unit Tests ===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;compute_weighted_score&amp;quot; test can be found in the Questionnaire Unit Test file.&lt;br /&gt;
&lt;br /&gt;
== General Information ==&lt;br /&gt;
&lt;br /&gt;
=== Members ===&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
=== Highlights of Project ===&lt;br /&gt;
* Added unit test for questionnaire class&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
=== List of files changed ===&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= NOTE =&lt;br /&gt;
Questionnaire class, has_many :assignment_questionnaire needs to be :assignment_questionnaires.  Also needs to be changed on the next line: through: =&amp;gt; &amp;quot;assignment_questionnaires&amp;quot; (we were told that this was convention to be plural).  If this is the case, then in the &amp;quot;compute_weighted_score&amp;quot; method, assignemnt_questionnaire also needs to be changed to assignment_questionnaires.  This most likely needs to be refactored, but we did not want to do a refactor without testing its effects on the system.&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74032</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74032"/>
		<updated>2013-03-20T19:44:54Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* List of files changed */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
&lt;br /&gt;
== QuestionnaireController Functional Tests ==&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing QuestionnaireController Test ===&lt;br /&gt;
&lt;br /&gt;
There were a few tests already written for the Questionnaire controller, but they were not passing initially. We had to modify several things in the test and questionnaire fixture in order to get things up and running. The setup method contained login code that was outdated, and thus each test would result in a redirect due to the invalid login credential. So we looked through other more up to date functional test files and found the correct way to login as a particular user. We also put this code in a helper method so we can call it more easily in different tests if we so choose (more on this in the &amp;quot;Helper Methods&amp;quot; section).&lt;br /&gt;
&lt;br /&gt;
In addition, we added a new questionnaire fixture (:questionnaire5) to help with our testing. Through debugging the test error messages, we saw that an additional required column called &amp;quot;section&amp;quot; has been added to the &amp;quot;Questionnaire&amp;quot; model, which was contributing to the failures. We added the additional column accordingly in :questionnaire5.&lt;br /&gt;
&lt;br /&gt;
=== Helper Methods ===&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
* Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
* Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
* Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
=== How to Run Controller Test ===&lt;br /&gt;
&lt;br /&gt;
All of the QuestionnaireController tests are in the questionnaire_controller_test.rb file and can be run as a standard functional test.&lt;br /&gt;
&lt;br /&gt;
== Questionnaire Subclass Unit Tests ==&lt;br /&gt;
&lt;br /&gt;
The original assignment was to write unit tests for the Questionnaire Subclasses. As we reviewed the tests, it was found that most of the methods were trivial (ex: the method &amp;quot;symbol&amp;quot; only only returns a hardcoded symbol).  The only test that was found to be worth testing was the get_weighted_score method.  Upon further review, however, we found that testing this method in each of the four subclasses violated the DRY principle.  Therefore, we moved the test to the superclass (Questionnaire) to test the &amp;quot;get_weighted_score&amp;quot; method.&lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing Questionnaire Subclass Tests ===&lt;br /&gt;
&lt;br /&gt;
There was only one test in the existing unit test for AuthorFeedbackQuestionnaire for testing the &amp;quot;get_weighted_score&amp;quot; method. Even though the test could be run, we found that it was not making assertions as it should. The files for the other 3 questionnaire subclasses were created to complete the testing on them.  As stated earlier, we then found that testing the &amp;quot;get_weighted_score&amp;quot; method was redundant, violating the DRY principle, so it was moved to the superclass (Questionnaire).  The tests in the subclasses were deleted (since they were redundant) but the test files were kept for future testing of the subclass if/when needed.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
Tested the one method &amp;quot;compute_weighted_score&amp;quot; from the subclasses that was not trivial. Based on an average, it was asserted that the value was not nil and that the value was computed correctly.&lt;br /&gt;
&lt;br /&gt;
=== How to Run Unit Tests ===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;compute_weighted_score&amp;quot; test can be found in the Questionnaire Unit Test file.&lt;br /&gt;
&lt;br /&gt;
== General Information ==&lt;br /&gt;
&lt;br /&gt;
=== Members ===&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
=== Highlights of Project ===&lt;br /&gt;
* Added unit test for questionnaire class&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
=== List of files changed ===&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Questionnaire class, has_many :assignment_questionnaire needs to be :assignment_questionnaires.  Also needs to be changed on the next line: through: =&amp;gt; &amp;quot;assignment_questionnaires&amp;quot; (we were told that this was convention to be plural).  If this is the case, then in the &amp;quot;compute_weighted_score&amp;quot; method, assignemnt_questionnaire also needs to be changed to assignment_questionnaires.  This most likely needs to be refactored, but we did not want to do a refactor without testing its effects on the system.&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74031</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74031"/>
		<updated>2013-03-20T19:43:50Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
&lt;br /&gt;
== QuestionnaireController Functional Tests ==&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing QuestionnaireController Test ===&lt;br /&gt;
&lt;br /&gt;
There were a few tests already written for the Questionnaire controller, but they were not passing initially. We had to modify several things in the test and questionnaire fixture in order to get things up and running. The setup method contained login code that was outdated, and thus each test would result in a redirect due to the invalid login credential. So we looked through other more up to date functional test files and found the correct way to login as a particular user. We also put this code in a helper method so we can call it more easily in different tests if we so choose (more on this in the &amp;quot;Helper Methods&amp;quot; section).&lt;br /&gt;
&lt;br /&gt;
In addition, we added a new questionnaire fixture (:questionnaire5) to help with our testing. Through debugging the test error messages, we saw that an additional required column called &amp;quot;section&amp;quot; has been added to the &amp;quot;Questionnaire&amp;quot; model, which was contributing to the failures. We added the additional column accordingly in :questionnaire5.&lt;br /&gt;
&lt;br /&gt;
=== Helper Methods ===&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
* Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
* Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
* Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
=== How to Run Controller Test ===&lt;br /&gt;
&lt;br /&gt;
All of the QuestionnaireController tests are in the questionnaire_controller_test.rb file and can be run as a standard functional test.&lt;br /&gt;
&lt;br /&gt;
== Questionnaire Subclass Unit Tests ==&lt;br /&gt;
&lt;br /&gt;
The original assignment was to write unit tests for the Questionnaire Subclasses. As we reviewed the tests, it was found that most of the methods were trivial (ex: the method &amp;quot;symbol&amp;quot; only only returns a hardcoded symbol).  The only test that was found to be worth testing was the get_weighted_score method.  Upon further review, however, we found that testing this method in each of the four subclasses violated the DRY principle.  Therefore, we moved the test to the superclass (Questionnaire) to test the &amp;quot;get_weighted_score&amp;quot; method.&lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing Questionnaire Subclass Tests ===&lt;br /&gt;
&lt;br /&gt;
There was only one test in the existing unit test for AuthorFeedbackQuestionnaire for testing the &amp;quot;get_weighted_score&amp;quot; method. Even though the test could be run, we found that it was not making assertions as it should. The files for the other 3 questionnaire subclasses were created to complete the testing on them.  As stated earlier, we then found that testing the &amp;quot;get_weighted_score&amp;quot; method was redundant, violating the DRY principle, so it was moved to the superclass (Questionnaire).  The tests in the subclasses were deleted (since they were redundant) but the test files were kept for future testing of the subclass if/when needed.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
Tested the one method &amp;quot;compute_weighted_score&amp;quot; from the subclasses that was not trivial. Based on an average, it was asserted that the value was not nil and that the value was computed correctly.&lt;br /&gt;
&lt;br /&gt;
=== How to Run Unit Tests ===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;compute_weighted_score&amp;quot; test can be found in the Questionnaire Unit Test file.&lt;br /&gt;
&lt;br /&gt;
== General Information ==&lt;br /&gt;
&lt;br /&gt;
=== Members ===&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
=== Highlights of Project ===&lt;br /&gt;
* Added unit test for questionnaire class&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
=== List of files changed ===&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Questionnaire class, has_many :assignment_questionnaire needs to be :assignment_questionnaires.  Also needs to be changed on the next line: through: =&amp;gt; &amp;quot;assignment_questionnaires&amp;quot; (we were told that this was ).  If this is the case, then in the &amp;quot;compute_weighted_score&amp;quot; method, assignemnt_questionnaire also needs to be changed to assignment_questionnaires.  This most likely needs to be refactored, but we did not want to do a refactor without testing its effects on the system.&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74030</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74030"/>
		<updated>2013-03-20T19:11:34Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* Test Coverage */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
&lt;br /&gt;
== QuestionnaireController Functional Tests ==&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing QuestionnaireController Test ===&lt;br /&gt;
&lt;br /&gt;
There were a few tests already written for the Questionnaire controller, but they were not passing initially. We had to modify several things in the test and questionnaire fixture in order to get things up and running. The setup method contained login code that was outdated, and thus each test would result in a redirect due to the invalid login credential. So we looked through other more up to date functional test files and found the correct way to login as a particular user. We also put this code in a helper method so we can call it more easily in different tests if we so choose (more on this in the &amp;quot;Helper Methods&amp;quot; section).&lt;br /&gt;
&lt;br /&gt;
In addition, we added a new questionnaire fixture (:questionnaire5) to help with our testing. Through debugging the test error messages, we saw that an additional required column called &amp;quot;section&amp;quot; has been added to the &amp;quot;Questionnaire&amp;quot; model, which was contributing to the failures. We added the additional column accordingly in :questionnaire5.&lt;br /&gt;
&lt;br /&gt;
=== Helper Methods ===&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
* Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
* Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
* Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
=== How to Run Controller Test ===&lt;br /&gt;
&lt;br /&gt;
All of the QuestionnaireController tests are in the questionnaire_controller_test.rb file and can be run as a standard functional test.&lt;br /&gt;
&lt;br /&gt;
== Questionnaire Subclass Unit Tests ==&lt;br /&gt;
&lt;br /&gt;
The original assignment was to write unit tests for the Questionnaire Subclasses. As we reviewed the tests, it was found that most of the methods were trivial (ex: the method &amp;quot;symbol&amp;quot; only only returns a hardcoded symbol).  The only test that was found to be worth testing was the get_weighted_score method.  Upon further review, however, we found that testing this method in each of the four subclasses violated the DRY principle.  Therefore, we moved the test to the superclass (Questionnaire) to test the &amp;quot;get_weighted_score&amp;quot; method.&lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing Questionnaire Subclass Tests ===&lt;br /&gt;
&lt;br /&gt;
There was only one test in the existing unit test for AuthorFeedbackQuestionnaire for testing the &amp;quot;get_weighted_score&amp;quot; method. Even though the test could be run, we found that it was not making assertions as it should. The files for the other 3 questionnaire subclasses were created to complete the testing on them.  As stated earlier, we then found that testing the &amp;quot;get_weighted_score&amp;quot; method was redundant, violating the DRY principle, so it was moved to the superclass (Questionnaire).  The tests in the subclasses were deleted (since they were redundant) but the test files were kept for future testing of the subclass if/when needed.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
Tested the one method &amp;quot;compute_weighted_score&amp;quot; from the subclasses that was not trivial. Based on an average, it was asserted that the value was not nil and that the value was computed correctly.&lt;br /&gt;
&lt;br /&gt;
=== How to Run Unit Tests ===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;compute_weighted_score&amp;quot; test can be found in the Questionnaire Unit Test file.&lt;br /&gt;
&lt;br /&gt;
== General Information ==&lt;br /&gt;
&lt;br /&gt;
=== Members ===&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
=== Highlights of Project ===&lt;br /&gt;
* Added unit test for questionnaire class&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
=== List of files changed ===&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74029</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74029"/>
		<updated>2013-03-20T19:09:46Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* Highlights of Project */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
&lt;br /&gt;
== QuestionnaireController Functional Tests ==&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing QuestionnaireController Test ===&lt;br /&gt;
&lt;br /&gt;
There were a few tests already written for the Questionnaire controller, but they were not passing initially. We had to modify several things in the test and questionnaire fixture in order to get things up and running. The setup method contained login code that was outdated, and thus each test would result in a redirect due to the invalid login credential. So we looked through other more up to date functional test files and found the correct way to login as a particular user. We also put this code in a helper method so we can call it more easily in different tests if we so choose (more on this in the &amp;quot;Helper Methods&amp;quot; section).&lt;br /&gt;
&lt;br /&gt;
In addition, we added a new questionnaire fixture (:questionnaire5) to help with our testing. Through debugging the test error messages, we saw that an additional required column called &amp;quot;section&amp;quot; has been added to the &amp;quot;Questionnaire&amp;quot; model, which was contributing to the failures. We added the additional column accordingly in :questionnaire5.&lt;br /&gt;
&lt;br /&gt;
=== Helper Methods ===&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
* Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
* Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
* Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
=== How to Run Controller Test ===&lt;br /&gt;
&lt;br /&gt;
All of the QuestionnaireController tests are in the questionnaire_controller_test.rb file and can be run as a standard functional test.&lt;br /&gt;
&lt;br /&gt;
== Questionnaire Subclass Unit Tests ==&lt;br /&gt;
&lt;br /&gt;
The original assignment was to write unit tests for the Questionnaire Subclasses. As we reviewed the tests, it was found that most of the methods were trivial (ex: the method &amp;quot;symbol&amp;quot; only only returns a hardcoded symbol).  The only test that was found to be worth testing was the get_weighted_score method.  Upon further review, however, we found that testing this method in each of the four subclasses violated the DRY principle.  Therefore, we moved the test to the superclass (Questionnaire) to test the &amp;quot;get_weighted_score&amp;quot; method.&lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing Questionnaire Subclass Tests ===&lt;br /&gt;
&lt;br /&gt;
There was only one test in the existing unit test for AuthorFeedbackQuestionnaire for testing the &amp;quot;get_weighted_score&amp;quot; method. Even though the test could be run, we found that it was not making assertions as it should. The files for the other 3 questionnaire subclasses were created to complete the testing on them.  As stated earlier, we then found that testing the &amp;quot;get_weighted_score&amp;quot; method was redundant, violating the DRY principle, so it was moved to the superclass (Questionnaire).  The tests in the subclasses were deleted (since they were redundant) but the test files were kept for future testing of the subclass if/when needed.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== How to Run Unit Tests ===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;compute_weighted_score&amp;quot; test can be found in the Questionnaire Unit Test file.&lt;br /&gt;
&lt;br /&gt;
== General Information ==&lt;br /&gt;
&lt;br /&gt;
=== Members ===&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
=== Highlights of Project ===&lt;br /&gt;
* Added unit test for questionnaire class&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
=== List of files changed ===&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74028</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74028"/>
		<updated>2013-03-20T19:09:17Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* How to Run Unit Tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
&lt;br /&gt;
== QuestionnaireController Functional Tests ==&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing QuestionnaireController Test ===&lt;br /&gt;
&lt;br /&gt;
There were a few tests already written for the Questionnaire controller, but they were not passing initially. We had to modify several things in the test and questionnaire fixture in order to get things up and running. The setup method contained login code that was outdated, and thus each test would result in a redirect due to the invalid login credential. So we looked through other more up to date functional test files and found the correct way to login as a particular user. We also put this code in a helper method so we can call it more easily in different tests if we so choose (more on this in the &amp;quot;Helper Methods&amp;quot; section).&lt;br /&gt;
&lt;br /&gt;
In addition, we added a new questionnaire fixture (:questionnaire5) to help with our testing. Through debugging the test error messages, we saw that an additional required column called &amp;quot;section&amp;quot; has been added to the &amp;quot;Questionnaire&amp;quot; model, which was contributing to the failures. We added the additional column accordingly in :questionnaire5.&lt;br /&gt;
&lt;br /&gt;
=== Helper Methods ===&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
* Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
* Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
* Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
=== How to Run Controller Test ===&lt;br /&gt;
&lt;br /&gt;
All of the QuestionnaireController tests are in the questionnaire_controller_test.rb file and can be run as a standard functional test.&lt;br /&gt;
&lt;br /&gt;
== Questionnaire Subclass Unit Tests ==&lt;br /&gt;
&lt;br /&gt;
The original assignment was to write unit tests for the Questionnaire Subclasses. As we reviewed the tests, it was found that most of the methods were trivial (ex: the method &amp;quot;symbol&amp;quot; only only returns a hardcoded symbol).  The only test that was found to be worth testing was the get_weighted_score method.  Upon further review, however, we found that testing this method in each of the four subclasses violated the DRY principle.  Therefore, we moved the test to the superclass (Questionnaire) to test the &amp;quot;get_weighted_score&amp;quot; method.&lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing Questionnaire Subclass Tests ===&lt;br /&gt;
&lt;br /&gt;
There was only one test in the existing unit test for AuthorFeedbackQuestionnaire for testing the &amp;quot;get_weighted_score&amp;quot; method. Even though the test could be run, we found that it was not making assertions as it should. The files for the other 3 questionnaire subclasses were created to complete the testing on them.  As stated earlier, we then found that testing the &amp;quot;get_weighted_score&amp;quot; method was redundant, violating the DRY principle, so it was moved to the superclass (Questionnaire).  The tests in the subclasses were deleted (since they were redundant) but the test files were kept for future testing of the subclass if/when needed.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== How to Run Unit Tests ===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;compute_weighted_score&amp;quot; test can be found in the Questionnaire Unit Test file.&lt;br /&gt;
&lt;br /&gt;
== General Information ==&lt;br /&gt;
&lt;br /&gt;
=== Members ===&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
=== Highlights of Project ===&lt;br /&gt;
* Added unit tests for questionnaire subclasses&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
=== List of files changed ===&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74027</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74027"/>
		<updated>2013-03-20T19:07:32Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
&lt;br /&gt;
== QuestionnaireController Functional Tests ==&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing QuestionnaireController Test ===&lt;br /&gt;
&lt;br /&gt;
There were a few tests already written for the Questionnaire controller, but they were not passing initially. We had to modify several things in the test and questionnaire fixture in order to get things up and running. The setup method contained login code that was outdated, and thus each test would result in a redirect due to the invalid login credential. So we looked through other more up to date functional test files and found the correct way to login as a particular user. We also put this code in a helper method so we can call it more easily in different tests if we so choose (more on this in the &amp;quot;Helper Methods&amp;quot; section).&lt;br /&gt;
&lt;br /&gt;
In addition, we added a new questionnaire fixture (:questionnaire5) to help with our testing. Through debugging the test error messages, we saw that an additional required column called &amp;quot;section&amp;quot; has been added to the &amp;quot;Questionnaire&amp;quot; model, which was contributing to the failures. We added the additional column accordingly in :questionnaire5.&lt;br /&gt;
&lt;br /&gt;
=== Helper Methods ===&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
* Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
* Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
* Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
=== How to Run Controller Test ===&lt;br /&gt;
&lt;br /&gt;
All of the QuestionnaireController tests are in the questionnaire_controller_test.rb file and can be run as a standard functional test.&lt;br /&gt;
&lt;br /&gt;
== Questionnaire Subclass Unit Tests ==&lt;br /&gt;
&lt;br /&gt;
The original assignment was to write unit tests for the Questionnaire Subclasses. As we reviewed the tests, it was found that most of the methods were trivial (ex: the method &amp;quot;symbol&amp;quot; only only returns a hardcoded symbol).  The only test that was found to be worth testing was the get_weighted_score method.  Upon further review, however, we found that testing this method in each of the four subclasses violated the DRY principle.  Therefore, we moved the test to the superclass (Questionnaire) to test the &amp;quot;get_weighted_score&amp;quot; method.&lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing Questionnaire Subclass Tests ===&lt;br /&gt;
&lt;br /&gt;
There was only one test in the existing unit test for AuthorFeedbackQuestionnaire for testing the &amp;quot;get_weighted_score&amp;quot; method. Even though the test could be run, we found that it was not making assertions as it should. The files for the other 3 questionnaire subclasses were created to complete the testing on them.  As stated earlier, we then found that testing the &amp;quot;get_weighted_score&amp;quot; method was redundant, violating the DRY principle, so it was moved to the superclass (Questionnaire).  The tests in the subclasses were deleted (since they were redundant) but the test files were kept for future testing of the subclass if/when needed.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== How to Run Unit Tests ===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;compute_weighted_score&amp;quot; test can be found in the questionnaire unit test file.&lt;br /&gt;
&lt;br /&gt;
== General Information ==&lt;br /&gt;
&lt;br /&gt;
=== Members ===&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
=== Highlights of Project ===&lt;br /&gt;
* Added unit tests for questionnaire subclasses&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
=== List of files changed ===&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74026</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74026"/>
		<updated>2013-03-20T19:07:07Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* Questionnaire Subclass Unit Tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
&lt;br /&gt;
== QuestionnaireController Functional Tests ==&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing QuestionnaireController Test ===&lt;br /&gt;
&lt;br /&gt;
There were a few tests already written for the Questionnaire controller, but they were not passing initially. We had to modify several things in the test and questionnaire fixture in order to get things up and running. The setup method contained login code that was outdated, and thus each test would result in a redirect due to the invalid login credential. So we looked through other more up to date functional test files and found the correct way to login as a particular user. We also put this code in a helper method so we can call it more easily in different tests if we so choose (more on this in the &amp;quot;Helper Methods&amp;quot; section).&lt;br /&gt;
&lt;br /&gt;
In addition, we added a new questionnaire fixture (:questionnaire5) to help with our testing. Through debugging the test error messages, we saw that an additional required column called &amp;quot;section&amp;quot; has been added to the &amp;quot;Questionnaire&amp;quot; model, which was contributing to the failures. We added the additional column accordingly in :questionnaire5.&lt;br /&gt;
&lt;br /&gt;
=== Helper Methods ===&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
* Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
* Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
* Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
=== How to Run Controller Test ===&lt;br /&gt;
&lt;br /&gt;
All of the QuestionnaireController tests are in the questionnaire_controller_test.rb file and can be run as a standard functional test.&lt;br /&gt;
&lt;br /&gt;
== Questionnaire Subclass Unit Tests ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The original assignment was to write unit tests for the Questionnaire Subclasses. As we reviewed the tests, it was found that most of the methods were trivial (ex: the method &amp;quot;symbol&amp;quot; only only returns a hardcoded symbol).  The only test that was found to be worth testing was the get_weighted_score method.  Upon further review, however, we found that testing this method in each of the four subclasses violated the DRY principle.  Therefore, we moved the test to the superclass (Questionnaire) to test the &amp;quot;get_weighted_score&amp;quot; method. &lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing Questionnaire Subclass Tests ===&lt;br /&gt;
&lt;br /&gt;
There was only one test in the existing unit test for AuthorFeedbackQuestionnaire for testing the &amp;quot;get_weighted_score&amp;quot; method. Even though the test could be run, we found that it was not making assertions as it should. The files for the other 3 questionnaire subclasses were created to complete the testing on them.  As stated earlier, we then found that testing the &amp;quot;get_weighted_score&amp;quot; method was redundant, violating the DRY principle, so it was moved to the superclass (Questionnaire).  The tests in the subclasses were deleted (since they were redundant) but the test files were kept for future testing of the subclass if/when needed.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== How to Run Unit Tests ===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;compute_weighted_score&amp;quot; test can be found in the questionnaire unit test file.&lt;br /&gt;
&lt;br /&gt;
== General Information ==&lt;br /&gt;
&lt;br /&gt;
=== Members ===&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
=== Highlights of Project ===&lt;br /&gt;
* Added unit tests for questionnaire subclasses&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
=== List of files changed ===&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74025</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74025"/>
		<updated>2013-03-20T19:05:38Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* Questionnaire Subclass Unit Tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
&lt;br /&gt;
== QuestionnaireController Functional Tests ==&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing QuestionnaireController Test ===&lt;br /&gt;
&lt;br /&gt;
There were a few tests already written for the Questionnaire controller, but they were not passing initially. We had to modify several things in the test and questionnaire fixture in order to get things up and running. The setup method contained login code that was outdated, and thus each test would result in a redirect due to the invalid login credential. So we looked through other more up to date functional test files and found the correct way to login as a particular user. We also put this code in a helper method so we can call it more easily in different tests if we so choose (more on this in the &amp;quot;Helper Methods&amp;quot; section).&lt;br /&gt;
&lt;br /&gt;
In addition, we added a new questionnaire fixture (:questionnaire5) to help with our testing. Through debugging the test error messages, we saw that an additional required column called &amp;quot;section&amp;quot; has been added to the &amp;quot;Questionnaire&amp;quot; model, which was contributing to the failures. We added the additional column accordingly in :questionnaire5.&lt;br /&gt;
&lt;br /&gt;
=== Helper Methods ===&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
* Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
* Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
* Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
=== How to Run Controller Test ===&lt;br /&gt;
&lt;br /&gt;
All of the QuestionnaireController tests are in the questionnaire_controller_test.rb file and can be run as a standard functional test.&lt;br /&gt;
&lt;br /&gt;
== Questionnaire Subclass Unit Tests ==&lt;br /&gt;
&lt;br /&gt;
The original assignment was to write unit tests for the Questionnaire Subclasses. As we reviewed the tests, it was found that most of the methods were trivial (ex: the method &amp;quot;symbol&amp;quot; only only returns a hardcoded symbol).  The only test that was found to be worth testing was the get_weighted_score method.  Upon further review, however, we found that testing this method in each of the four subclasses violated the DRY principle.  Therefore, we moved the test to the superclass (Questionnaire) to test the &amp;quot;get_weighted_score&amp;quot; method. &lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing Questionnaire Subclass Tests ===&lt;br /&gt;
&lt;br /&gt;
There was only one test in the existing unit test for AuthorFeedbackQuestionnaire for testing the &amp;quot;get_weighted_score&amp;quot; method. Even though the test could be run, we found that it was not making assertions as it should. The files for the other 3 questionnaire subclasses were created to complete the testing on them.  As stated earlier, we then found that testing the &amp;quot;get_weighted_score&amp;quot; method was redundant, violating the DRY principle, so it was moved to the superclass (Questionnaire).  The tests in the subclasses were deleted (since they were redundant) but the test files were kept for future testing of the subclass if/when needed.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== How to Run Unit Tests ===&lt;br /&gt;
&lt;br /&gt;
== General Information ==&lt;br /&gt;
&lt;br /&gt;
=== Members ===&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
=== Highlights of Project ===&lt;br /&gt;
* Added unit tests for questionnaire subclasses&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
=== List of files changed ===&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73884</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73884"/>
		<updated>2013-03-15T02:19:17Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
==Members==&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
== Highlights ==&lt;br /&gt;
*unit tests for questionnaire subclasses&lt;br /&gt;
*functional test for questionnaire controller&lt;br /&gt;
*20+ total tests&lt;br /&gt;
*Delete test was failing due to a potential bug in the questionnaire controller.&lt;br /&gt;
&lt;br /&gt;
===List of files changed ==&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73883</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73883"/>
		<updated>2013-03-15T02:18:36Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
==Members==&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
== Highlights ==&lt;br /&gt;
*unit tests for questionnaire subclasses&lt;br /&gt;
*funtional test for questionnaire controller&lt;br /&gt;
*24 total tests&lt;br /&gt;
*Delete test was failing due to a potential bug in the questionnaire controller.&lt;br /&gt;
&lt;br /&gt;
===List of files changed ==&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73882</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73882"/>
		<updated>2013-03-15T01:56:57Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
==Members==&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73881</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73881"/>
		<updated>2013-03-15T01:56:37Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Testing - questionnaire&lt;br /&gt;
==Members==&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73880</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73880"/>
		<updated>2013-03-15T01:54:25Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Testing - questionnaire&lt;br /&gt;
&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73879</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73879"/>
		<updated>2013-03-15T01:53:49Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: Created page with &amp;quot;Testing - questionnaire  Jonathan Wills - jrwills2 Chun Sing Tsui - ctsui Travis Folsom - twfolsom&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Testing - questionnaire&lt;br /&gt;
&lt;br /&gt;
Jonathan Wills - jrwills2&lt;br /&gt;
Chun Sing Tsui - ctsui&lt;br /&gt;
Travis Folsom - twfolsom&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73701</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73701"/>
		<updated>2013-02-26T02:11:32Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* The MIN macro: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Create a new class at runtime&lt;br /&gt;
::*Create new methods at runtime&lt;br /&gt;
::*Modify existing classes and/or methods at runtime&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
*Ruby is a [http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing dynamically typed language]&lt;br /&gt;
&lt;br /&gt;
::In dynamically typed languages, the majority of its type checking is performed at run-time, as opposed to compile time.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing Wikipedia Article on the Type System]&amp;lt;/ref&amp;gt;  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.&lt;br /&gt;
&lt;br /&gt;
*Ruby is an [http://en.wikipedia.org/wiki/Interpreted_language interpreted language]&lt;br /&gt;
::Interpreted languages avoid explicit compilation&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Interpreted_language Wikipeida Article on Interpreted Language]&amp;lt;/ref&amp;gt;.  Because Ruby is being interpreted as it is being executed, it is possible to create new classes and methods during program execution.&lt;br /&gt;
&lt;br /&gt;
*Ruby classes are [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open]&lt;br /&gt;
&lt;br /&gt;
::Most object-oriented programming languages adhere to the [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open/closed principle]&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle Wikipedia Article on the Open/Closed Principle]&amp;lt;/ref&amp;gt;.  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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples of Metaprogramming in Ruby ==&lt;br /&gt;
&lt;br /&gt;
=== Creating a New Class at Runtime ===&lt;br /&gt;
Below is an example of creating a new class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         puts &amp;quot;MyClass defined&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In example shown above, the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method is invoked on the &amp;lt;tt&amp;gt;3&amp;lt;/tt&amp;gt; object.  Inside of the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method call, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is defined.  &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is actually defined three times, once for each of the three iterations.  Even though the class is defined three times, it has the same effect as only defining it once.  The important thing to note here, is that a new class is defined at runtime, inside of a method call.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Class at Runtime ===&lt;br /&gt;
Below is an example of modifying a class, and and example of creating a new method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         def one&lt;br /&gt;
            puts &amp;quot;one&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is modified again to include a new method.  This method, the &amp;lt;tt&amp;gt;one&amp;lt;/tt&amp;gt; method is defined three times, just like the class is defined three times.  Even though it has been defined three times, it has the same effect as only defining it once.  The important thing to note here is that a new method is defined at runtime.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Creating a Method at Runtime ===&lt;br /&gt;
Below is an additional example of modifying a class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class&amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is reopened and the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is added to it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Method at Runtime ===&lt;br /&gt;
&lt;br /&gt;
Below is an additional example of modifying a method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;one two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is reopened and modified.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the modified code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   one two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Responding to a Non-Existent Method Call ===&lt;br /&gt;
Whenever a call to an undefined method is made on an object, Ruby provides an option to intercept the call. This is done by implementing the method method_missing within the class definition.  Ruby passes, as parameters, the name of the method called and the arguments passed to it.  &lt;br /&gt;
&lt;br /&gt;
Define a module (or class) Roman.  This class contains a method_missing method that intercepts calls to “class methods” that are undefined.  It then tries to interpret the method name as a Roman numeral.&lt;br /&gt;
For example, &lt;br /&gt;
*evaluating Roman.xix calls the xix,method of module Roman.  &lt;br /&gt;
*Roman has no xix method, so method_missing is invoked with xix as the argument.  &lt;br /&gt;
*The id2name method of class Symbol is invoked on :xix, returning &amp;quot;xix&amp;quot;. &lt;br /&gt;
*The &amp;quot;xix&amp;quot; is then parsed according to the rules for evaluating Roman numerals, and evaluates to 19.&lt;br /&gt;
&lt;br /&gt;
  class Roman &lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    last = nil&lt;br /&gt;
    &lt;br /&gt;
  roman_string.to_s.upcase.split(//).reverse.inject(0) do&lt;br /&gt;
  |memo, digit|&lt;br /&gt;
      if digit_value = DIGITS[digit]&lt;br /&gt;
        if last &amp;amp;&amp;amp; last &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
  last = digit_value&lt;br /&gt;
      end&lt;br /&gt;
  memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== Additional Examples of Ruby Metaprogramming ===&lt;br /&gt;
&lt;br /&gt;
Ruby has several metaprogramming facilities.  One can create new class and add methods to it using &amp;quot;define_method&amp;quot;&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  c = Class.new&lt;br /&gt;
  c.class_eval do&lt;br /&gt;
    define_method :hi do&lt;br /&gt;
    puts &amp;quot;Hey&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  c.new.hi&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
This technique allows you to add any type of new code and modify behavior of a program at run time.&lt;br /&gt;
Another example, adding attr_accessor to all classes:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  Object.class_eval do&lt;br /&gt;
    class &amp;lt;&amp;lt; self&lt;br /&gt;
      def attribute_accessor( *instance_variables )  &lt;br /&gt;
        instance_variables.each do |instance_variable| &lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
            def #{instance_variable}&lt;br /&gt;
                @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
                @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ?&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
===The C preprocessor (CPP)&amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/library/l-metaprog1/index.html The Art of Metaprogramming]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
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.&lt;br /&gt;
=====The SWAP macro: =====&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type. A macro is effective in this situation because:&lt;br /&gt;
*A function call would take too much overhead for a simple operation&lt;br /&gt;
*The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers. &lt;br /&gt;
*A different function would need to be coded for each type of item you wanted to swap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  #define SWAP(a, b, type) { type __tmp_c; c = b; b = a; a = c; }&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
      int a = 3;&lt;br /&gt;
      int b = 5;&lt;br /&gt;
      printf(&amp;quot;a is %d and b is %d\n&amp;quot;, a, b);&lt;br /&gt;
      SWAP(a, b, int);&lt;br /&gt;
      printf(&amp;quot;a is now %d and b is now %d\n&amp;quot;, a, b);&lt;br /&gt;
      return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
===Meta Programming in Java&amp;lt;ref&amp;gt;[http://www.cs.tut.fi/~kk/webstuff/MetaProgrammingJavaKalvot.pdf Metaprogramming in Java]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
Two new types:&lt;br /&gt;
*Parametrized types&lt;br /&gt;
*Type variables&lt;br /&gt;
A type variable is an unqualified identifier.&lt;br /&gt;
Class and interface declarations can have type arguments (type variables)&lt;br /&gt;
*Metalevel: Class implements GenericsDeclaration&lt;br /&gt;
Method and constructors definitions can have type arguments (type variables)&lt;br /&gt;
*Metalevel: Method, Constructor implements GenericsDeclaration&lt;br /&gt;
&lt;br /&gt;
  public interface List&amp;lt;E&amp;gt; { &lt;br /&gt;
    void add(E x);&lt;br /&gt;
    Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
  }&lt;br /&gt;
  public interface Iterator&amp;lt;E&amp;gt; { &lt;br /&gt;
    E next();&lt;br /&gt;
    boolean hasNext();&lt;br /&gt;
  }&lt;br /&gt;
  List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
  anExample.add(”sdfdfss”);&lt;br /&gt;
  anExample.add(new Object()); // compile time error&lt;br /&gt;
  String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73700</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73700"/>
		<updated>2013-02-26T02:09:05Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* Responding to a Non-Existent Method Call */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Create a new class at runtime&lt;br /&gt;
::*Create new methods at runtime&lt;br /&gt;
::*Modify existing classes and/or methods at runtime&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
*Ruby is a [http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing dynamically typed language]&lt;br /&gt;
&lt;br /&gt;
::In dynamically typed languages, the majority of its type checking is performed at run-time, as opposed to compile time.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing Wikipedia Article on the Type System]&amp;lt;/ref&amp;gt;  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.&lt;br /&gt;
&lt;br /&gt;
*Ruby is an [http://en.wikipedia.org/wiki/Interpreted_language interpreted language]&lt;br /&gt;
::Interpreted languages avoid explicit compilation&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Interpreted_language Wikipeida Article on Interpreted Language]&amp;lt;/ref&amp;gt;.  Because Ruby is being interpreted as it is being executed, it is possible to create new classes and methods during program execution.&lt;br /&gt;
&lt;br /&gt;
*Ruby classes are [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open]&lt;br /&gt;
&lt;br /&gt;
::Most object-oriented programming languages adhere to the [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open/closed principle]&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle Wikipedia Article on the Open/Closed Principle]&amp;lt;/ref&amp;gt;.  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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples of Metaprogramming in Ruby ==&lt;br /&gt;
&lt;br /&gt;
=== Creating a New Class at Runtime ===&lt;br /&gt;
Below is an example of creating a new class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         puts &amp;quot;MyClass defined&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In example shown above, the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method is invoked on the &amp;lt;tt&amp;gt;3&amp;lt;/tt&amp;gt; object.  Inside of the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method call, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is defined.  &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is actually defined three times, once for each of the three iterations.  Even though the class is defined three times, it has the same effect as only defining it once.  The important thing to note here, is that a new class is defined at runtime, inside of a method call.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Class at Runtime ===&lt;br /&gt;
Below is an example of modifying a class, and and example of creating a new method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         def one&lt;br /&gt;
            puts &amp;quot;one&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is modified again to include a new method.  This method, the &amp;lt;tt&amp;gt;one&amp;lt;/tt&amp;gt; method is defined three times, just like the class is defined three times.  Even though it has been defined three times, it has the same effect as only defining it once.  The important thing to note here is that a new method is defined at runtime.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Creating a Method at Runtime ===&lt;br /&gt;
Below is an additional example of modifying a class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class&amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is reopened and the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is added to it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Method at Runtime ===&lt;br /&gt;
&lt;br /&gt;
Below is an additional example of modifying a method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;one two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is reopened and modified.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the modified code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   one two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Responding to a Non-Existent Method Call ===&lt;br /&gt;
Whenever a call to an undefined method is made on an object, Ruby provides an option to intercept the call. This is done by implementing the method method_missing within the class definition.  Ruby passes, as parameters, the name of the method called and the arguments passed to it.  &lt;br /&gt;
&lt;br /&gt;
Define a module (or class) Roman.  This class contains a method_missing method that intercepts calls to “class methods” that are undefined.  It then tries to interpret the method name as a Roman numeral.&lt;br /&gt;
For example, &lt;br /&gt;
*evaluating Roman.xix calls the xix,method of module Roman.  &lt;br /&gt;
*Roman has no xix method, so method_missing is invoked with xix as the argument.  &lt;br /&gt;
*The id2name method of class Symbol is invoked on :xix, returning &amp;quot;xix&amp;quot;. &lt;br /&gt;
*The &amp;quot;xix&amp;quot; is then parsed according to the rules for evaluating Roman numerals, and evaluates to 19.&lt;br /&gt;
&lt;br /&gt;
  class Roman &lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    last = nil&lt;br /&gt;
    &lt;br /&gt;
  roman_string.to_s.upcase.split(//).reverse.inject(0) do&lt;br /&gt;
  |memo, digit|&lt;br /&gt;
      if digit_value = DIGITS[digit]&lt;br /&gt;
        if last &amp;amp;&amp;amp; last &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
  last = digit_value&lt;br /&gt;
      end&lt;br /&gt;
  memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
=== Additional Examples of Ruby Metaprogramming ===&lt;br /&gt;
&lt;br /&gt;
Ruby has several metaprogramming facilities.  One can create new class and add methods to it using &amp;quot;define_method&amp;quot;&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  c = Class.new&lt;br /&gt;
  c.class_eval do&lt;br /&gt;
    define_method :hi do&lt;br /&gt;
    puts &amp;quot;Hey&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  c.new.hi&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
This technique allows you to add any type of new code and modify behavior of a program at run time.&lt;br /&gt;
Another example, adding attr_accessor to all classes:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  Object.class_eval do&lt;br /&gt;
    class &amp;lt;&amp;lt; self&lt;br /&gt;
      def attribute_accessor( *instance_variables )  &lt;br /&gt;
        instance_variables.each do |instance_variable| &lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
            def #{instance_variable}&lt;br /&gt;
                @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
                @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ?&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
===The C preprocessor (CPP)&amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/library/l-metaprog1/index.html The Art of Metaprogramming]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
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.&lt;br /&gt;
=====The SWAP macro: =====&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type. A macro is effective in this situation because:&lt;br /&gt;
*A function call would take too much overhead for a simple operation&lt;br /&gt;
*The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers. &lt;br /&gt;
*A different function would need to be coded for each type of item you wanted to swap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  #define SWAP(a, b, type) { type __tmp_c; c = b; b = a; a = c; }&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
      int a = 3;&lt;br /&gt;
      int b = 5;&lt;br /&gt;
      printf(&amp;quot;a is %d and b is %d\n&amp;quot;, a, b);&lt;br /&gt;
      SWAP(a, b, int);&lt;br /&gt;
      printf(&amp;quot;a is now %d and b is now %d\n&amp;quot;, a, b);&lt;br /&gt;
      return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====The MIN macro: =====&lt;br /&gt;
  #define MIN(x, y) ((x) &amp;gt; (y) ? (y) : (x))&lt;br /&gt;
&lt;br /&gt;
This macro returns the the minimum of two values.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Meta Programming in Java&amp;lt;ref&amp;gt;[http://www.cs.tut.fi/~kk/webstuff/MetaProgrammingJavaKalvot.pdf Metaprogramming in Java]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
Two new types:&lt;br /&gt;
*Parametrized types&lt;br /&gt;
*Type variables&lt;br /&gt;
A type variable is an unqualified identifier.&lt;br /&gt;
Class and interface declarations can have type arguments (type variables)&lt;br /&gt;
*Metalevel: Class implements GenericsDeclaration&lt;br /&gt;
Method and constructors definitions can have type arguments (type variables)&lt;br /&gt;
*Metalevel: Method, Constructor implements GenericsDeclaration&lt;br /&gt;
&lt;br /&gt;
  public interface List&amp;lt;E&amp;gt; { &lt;br /&gt;
    void add(E x);&lt;br /&gt;
    Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
  }&lt;br /&gt;
  public interface Iterator&amp;lt;E&amp;gt; { &lt;br /&gt;
    E next();&lt;br /&gt;
    boolean hasNext();&lt;br /&gt;
  }&lt;br /&gt;
  List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
  anExample.add(”sdfdfss”);&lt;br /&gt;
  anExample.add(new Object()); // compile time error&lt;br /&gt;
  String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73557</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73557"/>
		<updated>2013-02-21T04:17:47Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* MetaProgramming in JavaMetaprogramming in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Create a new class at runtime&lt;br /&gt;
::*Create new methods at runtime&lt;br /&gt;
::*Modify existing classes and/or methods at runtime&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
*Ruby is a [http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing dynamically typed language]&lt;br /&gt;
&lt;br /&gt;
::In dynamically typed languages, the majority of its type checking is performed at run-time, as opposed to compile time.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing Wikipedia Article on the Type System]&amp;lt;/ref&amp;gt;  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.&lt;br /&gt;
&lt;br /&gt;
*Ruby is an [http://en.wikipedia.org/wiki/Interpreted_language interpreted language]&lt;br /&gt;
::Interpreted languages avoid explicit compilation&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Interpreted_language Wikipeida Article on Interpreted Language]&amp;lt;/ref&amp;gt;.  Because Ruby is being interpreted as it is being executed, it is possible to create new classes and methods during program execution.&lt;br /&gt;
&lt;br /&gt;
*Ruby classes are [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open]&lt;br /&gt;
&lt;br /&gt;
::Most object-oriented programming languages adhere to the [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open/closed principle]&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle Wikipedia Article on the Open/Closed Principle]&amp;lt;/ref&amp;gt;.  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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples of Metaprogramming in Ruby ==&lt;br /&gt;
&lt;br /&gt;
=== Creating a New Class at Runtime ===&lt;br /&gt;
Below is an example of creating a new class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         puts &amp;quot;MyClass defined&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In example shown above, the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method is invoked on the &amp;lt;tt&amp;gt;3&amp;lt;/tt&amp;gt; object.  Inside of the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method call, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is defined.  &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is actually defined three times, once for each of the three iterations.  Even though the class is defined three times, it has the same effect as only defining it once.  The important thing to note here, is that a new class is defined at runtime, inside of a method call.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Class at Runtime ===&lt;br /&gt;
Below is an example of modifying a class, and and example of creating a new method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         def one&lt;br /&gt;
            puts &amp;quot;one&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is modified again to include a new method.  This method, the &amp;lt;tt&amp;gt;one&amp;lt;/tt&amp;gt; method is defined three times, just like the class is defined three times.  Even though it has been defined three times, it has the same effect as only defining it once.  The important thing to note here is that a new method is defined at runtime.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Creating a Method at Runtime ===&lt;br /&gt;
Below is an additional example of modifying a class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class&amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is reopened and the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is added to it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Method at Runtime ===&lt;br /&gt;
&lt;br /&gt;
Below is an additional example of modifying a method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;one two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is reopened and modified.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the modified code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   one two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Responding to a Non-Existent Method Call ===&lt;br /&gt;
Whenever a call to an undefined method is made on an object, Ruby provides an option to intercept the call. This is done by implementing the method method_missing within the class definition.  Ruby passes as parameters the name of the method called and the arguments passed to it. Another interesting use of method_missing can be found on the Web by looking for “Roman method_missing”. Define a module (or class) Roman.  This class contains a method_missing method that intercepts calls to “class methods” that are undefined.  It then tries to interpret the method name as a Roman numeral.&lt;br /&gt;
For example, &lt;br /&gt;
*evaluating Roman.xix calls the xix,method of module Roman.  &lt;br /&gt;
*Roman has no xix method, so method_missing is invoked with xix as the argument.  &lt;br /&gt;
*The id2name method of class Symbol is invoked on :xix, returning &amp;quot;xix&amp;quot;. &lt;br /&gt;
*The &amp;quot;xix&amp;quot; is then parsed according to the rules for evaluating Roman numerals, and evaluates to 19.&lt;br /&gt;
&lt;br /&gt;
  class Roman &lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    last = nil&lt;br /&gt;
    &lt;br /&gt;
  roman_string.to_s.upcase.split(//).reverse.inject(0) do&lt;br /&gt;
  |memo, digit|&lt;br /&gt;
      if digit_value = DIGITS[digit]&lt;br /&gt;
        if last &amp;amp;&amp;amp; last &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
  last = digit_value&lt;br /&gt;
      end&lt;br /&gt;
  memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby has several metaprogramming facilities.  One can create new class and add methods to it using &amp;quot;define_method&amp;quot;&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  c = Class.new&lt;br /&gt;
  c.class_eval do&lt;br /&gt;
    define_method :hi do&lt;br /&gt;
    puts &amp;quot;Hey&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  c.new.hi&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
This technique allows you to add any type of new code and modify behavior of a program at run time.&lt;br /&gt;
Another example, adding attr_accessor to all classes:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  Object.class_eval do&lt;br /&gt;
    class &amp;lt;&amp;lt; self&lt;br /&gt;
      def attribute_accessor( *instance_variables )  &lt;br /&gt;
        instance_variables.each do |instance_variable| &lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
            def #{instance_variable}&lt;br /&gt;
                @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
                @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ?&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
===The C preprocessor (CPP)&amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/library/l-metaprog1/index.html The Art of Metaprogramming]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
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.&lt;br /&gt;
=====The SWAP macro: =====&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type. A macro is effective in this situation because:&lt;br /&gt;
*A function call would take too much overhead for a simple operation&lt;br /&gt;
*The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers. &lt;br /&gt;
*A different function would need to be coded for each type of item you wanted to swap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  #define SWAP(a, b, type) { type __tmp_c; c = b; b = a; a = c; }&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
      int a = 3;&lt;br /&gt;
      int b = 5;&lt;br /&gt;
      printf(&amp;quot;a is %d and b is %d\n&amp;quot;, a, b);&lt;br /&gt;
      SWAP(a, b, int);&lt;br /&gt;
      printf(&amp;quot;a is now %d and b is now %d\n&amp;quot;, a, b);&lt;br /&gt;
      return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====The MIN macro: =====&lt;br /&gt;
  #define MIN(x, y) ((x) &amp;gt; (y) ? (y) : (x))&lt;br /&gt;
&lt;br /&gt;
This macro returns the the minimum of two values.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Meta Programming in Java&amp;lt;ref&amp;gt;[http://www.cs.tut.fi/~kk/webstuff/MetaProgrammingJavaKalvot.pdf Metaprogramming in Java]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
Two new types:&lt;br /&gt;
*Parametrized types&lt;br /&gt;
*Type variables&lt;br /&gt;
A type variable is an unqualified identifier.&lt;br /&gt;
Class and interface declarations can have type arguments (type variables)&lt;br /&gt;
*Metalevel: Class implements GenericsDeclaration&lt;br /&gt;
Method and constructors definitions can have type arguments (type variables)&lt;br /&gt;
*Metalevel: Method, Constructor implements GenericsDeclaration&lt;br /&gt;
&lt;br /&gt;
  public interface List&amp;lt;E&amp;gt; { &lt;br /&gt;
    void add(E x);&lt;br /&gt;
    Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
  }&lt;br /&gt;
  public interface Iterator&amp;lt;E&amp;gt; { &lt;br /&gt;
    E next();&lt;br /&gt;
    boolean hasNext();&lt;br /&gt;
  }&lt;br /&gt;
  List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
  anExample.add(”sdfdfss”);&lt;br /&gt;
  anExample.add(new Object()); // compile time error&lt;br /&gt;
  String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73556</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73556"/>
		<updated>2013-02-21T04:13:23Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* Metaprogramming in Other Languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Create a new class at runtime&lt;br /&gt;
::*Create new methods at runtime&lt;br /&gt;
::*Modify existing classes and/or methods at runtime&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
*Ruby is a [http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing dynamically typed language]&lt;br /&gt;
&lt;br /&gt;
::In dynamically typed languages, the majority of its type checking is performed at run-time, as opposed to compile time.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing Wikipedia Article on the Type System]&amp;lt;/ref&amp;gt;  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.&lt;br /&gt;
&lt;br /&gt;
*Ruby is an [http://en.wikipedia.org/wiki/Interpreted_language interpreted language]&lt;br /&gt;
::Interpreted languages avoid explicit compilation&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Interpreted_language Wikipeida Article on Interpreted Language]&amp;lt;/ref&amp;gt;.  Because Ruby is being interpreted as it is being executed, it is possible to create new classes and methods during program execution.&lt;br /&gt;
&lt;br /&gt;
*Ruby classes are [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open]&lt;br /&gt;
&lt;br /&gt;
::Most object-oriented programming languages adhere to the [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open/closed principle]&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle Wikipedia Article on the Open/Closed Principle]&amp;lt;/ref&amp;gt;.  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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples of Metaprogramming in Ruby ==&lt;br /&gt;
&lt;br /&gt;
=== Creating a New Class at Runtime ===&lt;br /&gt;
Below is an example of creating a new class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         puts &amp;quot;MyClass defined&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In example shown above, the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method is invoked on the &amp;lt;tt&amp;gt;3&amp;lt;/tt&amp;gt; object.  Inside of the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method call, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is defined.  &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is actually defined three times, once for each of the three iterations.  Even though the class is defined three times, it has the same effect as only defining it once.  The important thing to note here, is that a new class is defined at runtime, inside of a method call.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Class at Runtime ===&lt;br /&gt;
Below is an example of modifying a class, and and example of creating a new method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         def one&lt;br /&gt;
            puts &amp;quot;one&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is modified again to include a new method.  This method, the &amp;lt;tt&amp;gt;one&amp;lt;/tt&amp;gt; method is defined three times, just like the class is defined three times.  Even though it has been defined three times, it has the same effect as only defining it once.  The important thing to note here is that a new method is defined at runtime.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Creating a Method at Runtime ===&lt;br /&gt;
Below is an additional example of modifying a class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class&amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is reopened and the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is added to it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Method at Runtime ===&lt;br /&gt;
&lt;br /&gt;
Below is an additional example of modifying a method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;one two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is reopened and modified.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the modified code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   one two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Responding to a Non-Existent Method Call ===&lt;br /&gt;
Whenever a call to an undefined method is made on an object, Ruby provides an option to intercept the call. This is done by implementing the method method_missing within the class definition.  Ruby passes as parameters the name of the method called and the arguments passed to it. Another interesting use of method_missing can be found on the Web by looking for “Roman method_missing”. Define a module (or class) Roman.  This class contains a method_missing method that intercepts calls to “class methods” that are undefined.  It then tries to interpret the method name as a Roman numeral.&lt;br /&gt;
For example, &lt;br /&gt;
*evaluating Roman.xix calls the xix,method of module Roman.  &lt;br /&gt;
*Roman has no xix method, so method_missing is invoked with xix as the argument.  &lt;br /&gt;
*The id2name method of class Symbol is invoked on :xix, returning &amp;quot;xix&amp;quot;. &lt;br /&gt;
*The &amp;quot;xix&amp;quot; is then parsed according to the rules for evaluating Roman numerals, and evaluates to 19.&lt;br /&gt;
&lt;br /&gt;
  class Roman &lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    last = nil&lt;br /&gt;
    &lt;br /&gt;
  roman_string.to_s.upcase.split(//).reverse.inject(0) do&lt;br /&gt;
  |memo, digit|&lt;br /&gt;
      if digit_value = DIGITS[digit]&lt;br /&gt;
        if last &amp;amp;&amp;amp; last &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
  last = digit_value&lt;br /&gt;
      end&lt;br /&gt;
  memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby has several metaprogramming facilities.  One can create new class and add methods to it using &amp;quot;define_method&amp;quot;&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  c = Class.new&lt;br /&gt;
  c.class_eval do&lt;br /&gt;
    define_method :hi do&lt;br /&gt;
    puts &amp;quot;Hey&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  c.new.hi&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
This technique allows you to add any type of new code and modify behavior of a program at run time.&lt;br /&gt;
Another example, adding attr_accessor to all classes:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  Object.class_eval do&lt;br /&gt;
    class &amp;lt;&amp;lt; self&lt;br /&gt;
      def attribute_accessor( *instance_variables )  &lt;br /&gt;
        instance_variables.each do |instance_variable| &lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
            def #{instance_variable}&lt;br /&gt;
                @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
                @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ?&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
===The C preprocessor (CPP)&amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/library/l-metaprog1/index.html The Art of Metaprogramming]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
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.&lt;br /&gt;
=====The SWAP macro: =====&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type. A macro is effective in this situation because:&lt;br /&gt;
*A function call would take too much overhead for a simple operation&lt;br /&gt;
*The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers. &lt;br /&gt;
*A different function would need to be coded for each type of item you wanted to swap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  #define SWAP(a, b, type) { type __tmp_c; c = b; b = a; a = c; }&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
      int a = 3;&lt;br /&gt;
      int b = 5;&lt;br /&gt;
      printf(&amp;quot;a is %d and b is %d\n&amp;quot;, a, b);&lt;br /&gt;
      SWAP(a, b, int);&lt;br /&gt;
      printf(&amp;quot;a is now %d and b is now %d\n&amp;quot;, a, b);&lt;br /&gt;
      return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====The MIN macro: =====&lt;br /&gt;
  #define MIN(x, y) ((x) &amp;gt; (y) ? (y) : (x))&lt;br /&gt;
&lt;br /&gt;
This macro returns the the minimum of two values.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===MetaProgramming in Java&amp;lt;ref&amp;gt;[http://www.cs.tut.fi/~kk/webstuff/MetaProgrammingJavaKalvot.pdf Metaprogramming in Java]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73555</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73555"/>
		<updated>2013-02-21T04:08:05Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* Metaprogramming in Other Languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Create a new class at runtime&lt;br /&gt;
::*Create new methods at runtime&lt;br /&gt;
::*Modify existing classes and/or methods at runtime&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
*Ruby is a [http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing dynamically typed language]&lt;br /&gt;
&lt;br /&gt;
::In dynamically typed languages, the majority of its type checking is performed at run-time, as opposed to compile time.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing Wikipedia Article on the Type System]&amp;lt;/ref&amp;gt;  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.&lt;br /&gt;
&lt;br /&gt;
*Ruby is an [http://en.wikipedia.org/wiki/Interpreted_language interpreted language]&lt;br /&gt;
::Interpreted languages avoid explicit compilation&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Interpreted_language Wikipeida Article on Interpreted Language]&amp;lt;/ref&amp;gt;.  Because Ruby is being interpreted as it is being executed, it is possible to create new classes and methods during program execution.&lt;br /&gt;
&lt;br /&gt;
*Ruby classes are [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open]&lt;br /&gt;
&lt;br /&gt;
::Most object-oriented programming languages adhere to the [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open/closed principle]&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle Wikipedia Article on the Open/Closed Principle]&amp;lt;/ref&amp;gt;.  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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples of Metaprogramming in Ruby ==&lt;br /&gt;
&lt;br /&gt;
=== Creating a New Class at Runtime ===&lt;br /&gt;
Below is an example of creating a new class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         puts &amp;quot;MyClass defined&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In example shown above, the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method is invoked on the &amp;lt;tt&amp;gt;3&amp;lt;/tt&amp;gt; object.  Inside of the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method call, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is defined.  &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is actually defined three times, once for each of the three iterations.  Even though the class is defined three times, it has the same effect as only defining it once.  The important thing to note here, is that a new class is defined at runtime, inside of a method call.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Class at Runtime ===&lt;br /&gt;
Below is an example of modifying a class, and and example of creating a new method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         def one&lt;br /&gt;
            puts &amp;quot;one&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is modified again to include a new method.  This method, the &amp;lt;tt&amp;gt;one&amp;lt;/tt&amp;gt; method is defined three times, just like the class is defined three times.  Even though it has been defined three times, it has the same effect as only defining it once.  The important thing to note here is that a new method is defined at runtime.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Creating a Method at Runtime ===&lt;br /&gt;
Below is an additional example of modifying a class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class&amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is reopened and the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is added to it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Method at Runtime ===&lt;br /&gt;
&lt;br /&gt;
Below is an additional example of modifying a method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;one two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is reopened and modified.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the modified code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   one two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Responding to a Non-Existent Method Call ===&lt;br /&gt;
Whenever a call to an undefined method is made on an object, Ruby provides an option to intercept the call. This is done by implementing the method method_missing within the class definition.  Ruby passes as parameters the name of the method called and the arguments passed to it. Another interesting use of method_missing can be found on the Web by looking for “Roman method_missing”. Define a module (or class) Roman.  This class contains a method_missing method that intercepts calls to “class methods” that are undefined.  It then tries to interpret the method name as a Roman numeral.&lt;br /&gt;
For example, &lt;br /&gt;
*evaluating Roman.xix calls the xix,method of module Roman.  &lt;br /&gt;
*Roman has no xix method, so method_missing is invoked with xix as the argument.  &lt;br /&gt;
*The id2name method of class Symbol is invoked on :xix, returning &amp;quot;xix&amp;quot;. &lt;br /&gt;
*The &amp;quot;xix&amp;quot; is then parsed according to the rules for evaluating Roman numerals, and evaluates to 19.&lt;br /&gt;
&lt;br /&gt;
  class Roman &lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    last = nil&lt;br /&gt;
    &lt;br /&gt;
  roman_string.to_s.upcase.split(//).reverse.inject(0) do&lt;br /&gt;
  |memo, digit|&lt;br /&gt;
      if digit_value = DIGITS[digit]&lt;br /&gt;
        if last &amp;amp;&amp;amp; last &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
  last = digit_value&lt;br /&gt;
      end&lt;br /&gt;
  memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby has several metaprogramming facilities.  One can create new class and add methods to it using &amp;quot;define_method&amp;quot;&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  c = Class.new&lt;br /&gt;
  c.class_eval do&lt;br /&gt;
    define_method :hi do&lt;br /&gt;
    puts &amp;quot;Hey&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  c.new.hi&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
This technique allows you to add any type of new code and modify behavior of a program at run time.&lt;br /&gt;
Another example, adding attr_accessor to all classes:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  Object.class_eval do&lt;br /&gt;
    class &amp;lt;&amp;lt; self&lt;br /&gt;
      def attribute_accessor( *instance_variables )  &lt;br /&gt;
        instance_variables.each do |instance_variable| &lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
            def #{instance_variable}&lt;br /&gt;
                @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
                @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ?&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
===The C preprocessor (CPP)&amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/library/l-metaprog1/index.html The Art of Metaprogramming]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
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.&lt;br /&gt;
=====The SWAP macro: =====&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type. A macro is effective in this situation because:&lt;br /&gt;
*A function call would take too much overhead for a simple operation&lt;br /&gt;
*The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers. &lt;br /&gt;
*A different function would need to be coded for each type of item you wanted to swap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  #define SWAP(a, b, type) { type __tmp_c; c = b; b = a; a = c; }&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
      int a = 3;&lt;br /&gt;
      int b = 5;&lt;br /&gt;
      printf(&amp;quot;a is %d and b is %d\n&amp;quot;, a, b);&lt;br /&gt;
      SWAP(a, b, int);&lt;br /&gt;
      printf(&amp;quot;a is now %d and b is now %d\n&amp;quot;, a, b);&lt;br /&gt;
      return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====The MIN macro: =====&lt;br /&gt;
  #define MIN(x, y) ((x) &amp;gt; (y) ? (y) : (x))&lt;br /&gt;
&lt;br /&gt;
This macro returns the the minimum of two values.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The C preprocessor (CPP)&amp;lt;ref&amp;gt;[http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java The Art of Metaprogramming in Java]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73554</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73554"/>
		<updated>2013-02-21T04:05:53Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* Responding to a Non-Existent Method Call */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Create a new class at runtime&lt;br /&gt;
::*Create new methods at runtime&lt;br /&gt;
::*Modify existing classes and/or methods at runtime&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
*Ruby is a [http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing dynamically typed language]&lt;br /&gt;
&lt;br /&gt;
::In dynamically typed languages, the majority of its type checking is performed at run-time, as opposed to compile time.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing Wikipedia Article on the Type System]&amp;lt;/ref&amp;gt;  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.&lt;br /&gt;
&lt;br /&gt;
*Ruby is an [http://en.wikipedia.org/wiki/Interpreted_language interpreted language]&lt;br /&gt;
::Interpreted languages avoid explicit compilation&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Interpreted_language Wikipeida Article on Interpreted Language]&amp;lt;/ref&amp;gt;.  Because Ruby is being interpreted as it is being executed, it is possible to create new classes and methods during program execution.&lt;br /&gt;
&lt;br /&gt;
*Ruby classes are [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open]&lt;br /&gt;
&lt;br /&gt;
::Most object-oriented programming languages adhere to the [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open/closed principle]&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle Wikipedia Article on the Open/Closed Principle]&amp;lt;/ref&amp;gt;.  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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples of Metaprogramming in Ruby ==&lt;br /&gt;
&lt;br /&gt;
=== Creating a New Class at Runtime ===&lt;br /&gt;
Below is an example of creating a new class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         puts &amp;quot;MyClass defined&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In example shown above, the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method is invoked on the &amp;lt;tt&amp;gt;3&amp;lt;/tt&amp;gt; object.  Inside of the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method call, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is defined.  &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is actually defined three times, once for each of the three iterations.  Even though the class is defined three times, it has the same effect as only defining it once.  The important thing to note here, is that a new class is defined at runtime, inside of a method call.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Class at Runtime ===&lt;br /&gt;
Below is an example of modifying a class, and and example of creating a new method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         def one&lt;br /&gt;
            puts &amp;quot;one&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is modified again to include a new method.  This method, the &amp;lt;tt&amp;gt;one&amp;lt;/tt&amp;gt; method is defined three times, just like the class is defined three times.  Even though it has been defined three times, it has the same effect as only defining it once.  The important thing to note here is that a new method is defined at runtime.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Creating a Method at Runtime ===&lt;br /&gt;
Below is an additional example of modifying a class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class&amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is reopened and the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is added to it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Method at Runtime ===&lt;br /&gt;
&lt;br /&gt;
Below is an additional example of modifying a method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;one two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is reopened and modified.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the modified code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   one two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Responding to a Non-Existent Method Call ===&lt;br /&gt;
Whenever a call to an undefined method is made on an object, Ruby provides an option to intercept the call. This is done by implementing the method method_missing within the class definition.  Ruby passes as parameters the name of the method called and the arguments passed to it. Another interesting use of method_missing can be found on the Web by looking for “Roman method_missing”. Define a module (or class) Roman.  This class contains a method_missing method that intercepts calls to “class methods” that are undefined.  It then tries to interpret the method name as a Roman numeral.&lt;br /&gt;
For example, &lt;br /&gt;
*evaluating Roman.xix calls the xix,method of module Roman.  &lt;br /&gt;
*Roman has no xix method, so method_missing is invoked with xix as the argument.  &lt;br /&gt;
*The id2name method of class Symbol is invoked on :xix, returning &amp;quot;xix&amp;quot;. &lt;br /&gt;
*The &amp;quot;xix&amp;quot; is then parsed according to the rules for evaluating Roman numerals, and evaluates to 19.&lt;br /&gt;
&lt;br /&gt;
  class Roman &lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    last = nil&lt;br /&gt;
    &lt;br /&gt;
  roman_string.to_s.upcase.split(//).reverse.inject(0) do&lt;br /&gt;
  |memo, digit|&lt;br /&gt;
      if digit_value = DIGITS[digit]&lt;br /&gt;
        if last &amp;amp;&amp;amp; last &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
  last = digit_value&lt;br /&gt;
      end&lt;br /&gt;
  memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby has several metaprogramming facilities.  One can create new class and add methods to it using &amp;quot;define_method&amp;quot;&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  c = Class.new&lt;br /&gt;
  c.class_eval do&lt;br /&gt;
    define_method :hi do&lt;br /&gt;
    puts &amp;quot;Hey&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  c.new.hi&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
This technique allows you to add any type of new code and modify behavior of a program at run time.&lt;br /&gt;
Another example, adding attr_accessor to all classes:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  Object.class_eval do&lt;br /&gt;
    class &amp;lt;&amp;lt; self&lt;br /&gt;
      def attribute_accessor( *instance_variables )  &lt;br /&gt;
        instance_variables.each do |instance_variable| &lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
            def #{instance_variable}&lt;br /&gt;
                @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
                @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ?&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
===The C preprocessor (CPP)&amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/library/l-metaprog1/index.html The Art of Metaprogramming]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
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.&lt;br /&gt;
=====The SWAP macro: =====&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type. A macro is effective in this situation because:&lt;br /&gt;
*A function call would take too much overhead for a simple operation&lt;br /&gt;
*The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers. &lt;br /&gt;
*A different function would need to be coded for each type of item you wanted to swap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  #define SWAP(a, b, type) { type __tmp_c; c = b; b = a; a = c; }&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
      int a = 3;&lt;br /&gt;
      int b = 5;&lt;br /&gt;
      printf(&amp;quot;a is %d and b is %d\n&amp;quot;, a, b);&lt;br /&gt;
      SWAP(a, b, int);&lt;br /&gt;
      printf(&amp;quot;a is now %d and b is now %d\n&amp;quot;, a, b);&lt;br /&gt;
      return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====The MIN macro: =====&lt;br /&gt;
  #define MIN(x, y) ((x) &amp;gt; (y) ? (y) : (x))&lt;br /&gt;
&lt;br /&gt;
This macro returns the the minimum of two values.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73553</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73553"/>
		<updated>2013-02-21T04:05:34Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* Responding to a Non-Existent Method Call */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Create a new class at runtime&lt;br /&gt;
::*Create new methods at runtime&lt;br /&gt;
::*Modify existing classes and/or methods at runtime&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
*Ruby is a [http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing dynamically typed language]&lt;br /&gt;
&lt;br /&gt;
::In dynamically typed languages, the majority of its type checking is performed at run-time, as opposed to compile time.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing Wikipedia Article on the Type System]&amp;lt;/ref&amp;gt;  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.&lt;br /&gt;
&lt;br /&gt;
*Ruby is an [http://en.wikipedia.org/wiki/Interpreted_language interpreted language]&lt;br /&gt;
::Interpreted languages avoid explicit compilation&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Interpreted_language Wikipeida Article on Interpreted Language]&amp;lt;/ref&amp;gt;.  Because Ruby is being interpreted as it is being executed, it is possible to create new classes and methods during program execution.&lt;br /&gt;
&lt;br /&gt;
*Ruby classes are [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open]&lt;br /&gt;
&lt;br /&gt;
::Most object-oriented programming languages adhere to the [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open/closed principle]&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle Wikipedia Article on the Open/Closed Principle]&amp;lt;/ref&amp;gt;.  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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples of Metaprogramming in Ruby ==&lt;br /&gt;
&lt;br /&gt;
=== Creating a New Class at Runtime ===&lt;br /&gt;
Below is an example of creating a new class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         puts &amp;quot;MyClass defined&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In example shown above, the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method is invoked on the &amp;lt;tt&amp;gt;3&amp;lt;/tt&amp;gt; object.  Inside of the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method call, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is defined.  &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is actually defined three times, once for each of the three iterations.  Even though the class is defined three times, it has the same effect as only defining it once.  The important thing to note here, is that a new class is defined at runtime, inside of a method call.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Class at Runtime ===&lt;br /&gt;
Below is an example of modifying a class, and and example of creating a new method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         def one&lt;br /&gt;
            puts &amp;quot;one&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is modified again to include a new method.  This method, the &amp;lt;tt&amp;gt;one&amp;lt;/tt&amp;gt; method is defined three times, just like the class is defined three times.  Even though it has been defined three times, it has the same effect as only defining it once.  The important thing to note here is that a new method is defined at runtime.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Creating a Method at Runtime ===&lt;br /&gt;
Below is an additional example of modifying a class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class&amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is reopened and the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is added to it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Method at Runtime ===&lt;br /&gt;
&lt;br /&gt;
Below is an additional example of modifying a method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;one two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is reopened and modified.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the modified code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   one two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Responding to a Non-Existent Method Call ===&lt;br /&gt;
Whenever a call to an undefined method is made on an object, Ruby provides an option to intercept the call. This is done by implementing the method method_missing within the class definition.  Ruby passes as parameters the name of the method called and the arguments passed to it. Another interesting use of method_missing can be found on the Web by looking for “Roman method_missing”. Define a module (or class) Roman.  This class contains a method_missing method that intercepts calls to “class methods” that are undefined.  It then tries to interpret the method name as a Roman numeral.&lt;br /&gt;
For example, &lt;br /&gt;
*evaluating Roman.xix calls the xix,method of module Roman.  &lt;br /&gt;
*Roman has no xix method, so method_missing is invoked with xix as the argument.  &lt;br /&gt;
*The id2name method of class Symbol is invoked on :xix, returning &amp;quot;xix&amp;quot;. &lt;br /&gt;
*The &amp;quot;xix&amp;quot; is then parsed according to the rules for evaluating Roman numerals, and evaluates to 19.&lt;br /&gt;
&lt;br /&gt;
class Roman &lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    last = nil&lt;br /&gt;
    &lt;br /&gt;
  roman_string.to_s.upcase.split(//).reverse.inject(0) do&lt;br /&gt;
  |memo, digit|&lt;br /&gt;
      if digit_value = DIGITS[digit]&lt;br /&gt;
        if last &amp;amp;&amp;amp; last &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
  last = digit_value&lt;br /&gt;
      end&lt;br /&gt;
  memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby has several metaprogramming facilities.  One can create new class and add methods to it using &amp;quot;define_method&amp;quot;&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  c = Class.new&lt;br /&gt;
  c.class_eval do&lt;br /&gt;
    define_method :hi do&lt;br /&gt;
    puts &amp;quot;Hey&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  c.new.hi&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
This technique allows you to add any type of new code and modify behavior of a program at run time.&lt;br /&gt;
Another example, adding attr_accessor to all classes:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  Object.class_eval do&lt;br /&gt;
    class &amp;lt;&amp;lt; self&lt;br /&gt;
      def attribute_accessor( *instance_variables )  &lt;br /&gt;
        instance_variables.each do |instance_variable| &lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
            def #{instance_variable}&lt;br /&gt;
                @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
                @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ?&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
===The C preprocessor (CPP)&amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/library/l-metaprog1/index.html The Art of Metaprogramming]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
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.&lt;br /&gt;
=====The SWAP macro: =====&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type. A macro is effective in this situation because:&lt;br /&gt;
*A function call would take too much overhead for a simple operation&lt;br /&gt;
*The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers. &lt;br /&gt;
*A different function would need to be coded for each type of item you wanted to swap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  #define SWAP(a, b, type) { type __tmp_c; c = b; b = a; a = c; }&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
      int a = 3;&lt;br /&gt;
      int b = 5;&lt;br /&gt;
      printf(&amp;quot;a is %d and b is %d\n&amp;quot;, a, b);&lt;br /&gt;
      SWAP(a, b, int);&lt;br /&gt;
      printf(&amp;quot;a is now %d and b is now %d\n&amp;quot;, a, b);&lt;br /&gt;
      return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====The MIN macro: =====&lt;br /&gt;
  #define MIN(x, y) ((x) &amp;gt; (y) ? (y) : (x))&lt;br /&gt;
&lt;br /&gt;
This macro returns the the minimum of two values.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73552</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73552"/>
		<updated>2013-02-21T04:04:14Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* Responding to a Non-Existent Method Call */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Create a new class at runtime&lt;br /&gt;
::*Create new methods at runtime&lt;br /&gt;
::*Modify existing classes and/or methods at runtime&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
*Ruby is a [http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing dynamically typed language]&lt;br /&gt;
&lt;br /&gt;
::In dynamically typed languages, the majority of its type checking is performed at run-time, as opposed to compile time.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing Wikipedia Article on the Type System]&amp;lt;/ref&amp;gt;  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.&lt;br /&gt;
&lt;br /&gt;
*Ruby is an [http://en.wikipedia.org/wiki/Interpreted_language interpreted language]&lt;br /&gt;
::Interpreted languages avoid explicit compilation&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Interpreted_language Wikipeida Article on Interpreted Language]&amp;lt;/ref&amp;gt;.  Because Ruby is being interpreted as it is being executed, it is possible to create new classes and methods during program execution.&lt;br /&gt;
&lt;br /&gt;
*Ruby classes are [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open]&lt;br /&gt;
&lt;br /&gt;
::Most object-oriented programming languages adhere to the [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open/closed principle]&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle Wikipedia Article on the Open/Closed Principle]&amp;lt;/ref&amp;gt;.  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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples of Metaprogramming in Ruby ==&lt;br /&gt;
&lt;br /&gt;
=== Creating a New Class at Runtime ===&lt;br /&gt;
Below is an example of creating a new class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         puts &amp;quot;MyClass defined&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In example shown above, the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method is invoked on the &amp;lt;tt&amp;gt;3&amp;lt;/tt&amp;gt; object.  Inside of the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method call, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is defined.  &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is actually defined three times, once for each of the three iterations.  Even though the class is defined three times, it has the same effect as only defining it once.  The important thing to note here, is that a new class is defined at runtime, inside of a method call.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Class at Runtime ===&lt;br /&gt;
Below is an example of modifying a class, and and example of creating a new method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         def one&lt;br /&gt;
            puts &amp;quot;one&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is modified again to include a new method.  This method, the &amp;lt;tt&amp;gt;one&amp;lt;/tt&amp;gt; method is defined three times, just like the class is defined three times.  Even though it has been defined three times, it has the same effect as only defining it once.  The important thing to note here is that a new method is defined at runtime.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Creating a Method at Runtime ===&lt;br /&gt;
Below is an additional example of modifying a class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class&amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is reopened and the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is added to it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Method at Runtime ===&lt;br /&gt;
&lt;br /&gt;
Below is an additional example of modifying a method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;one two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is reopened and modified.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the modified code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   one two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Responding to a Non-Existent Method Call ===&lt;br /&gt;
Whenever a call to an undefined method is made on an object, Ruby provides an option to intercept the call. This is done by implementing the method method_missing within the class definition.  Ruby passes as parameters the name of the method called and the arguments passed to it. Another interesting use of method_missing can be found on the Web by looking for “Roman method_missing”. Define a module (or class) Roman.  This class contains a method_missing method that intercepts calls to “class methods” that are undefined.  It then tries to interpret the method name as a Roman numeral.&lt;br /&gt;
For example, &lt;br /&gt;
*evaluating Roman.xix calls the xix,method of module Roman.  &lt;br /&gt;
*Roman has no xix method, so method_missing is invoked with xix as the argument.  &lt;br /&gt;
*The id2name method of class Symbol is invoked on :xix, returning &amp;quot;xix&amp;quot;. &lt;br /&gt;
*The &amp;quot;xix&amp;quot; is then parsed according to the rules for evaluating Roman numerals, and evaluates to 19.&lt;br /&gt;
&lt;br /&gt;
class Roman &lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,eek 2 Object-Oriented Languages and Systems 23&lt;br /&gt;
  CSC 517 23&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    last = nil&lt;br /&gt;
    &lt;br /&gt;
  roman_string.to_s.upcase.split(//).reverse.inject(0) do&lt;br /&gt;
  |memo, digit|&lt;br /&gt;
      if digit_value = DIGITS[digit]&lt;br /&gt;
        if last &amp;amp;&amp;amp; last &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
  last = digit_value&lt;br /&gt;
      end&lt;br /&gt;
  memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby has several metaprogramming facilities.  One can create new class and add methods to it using &amp;quot;define_method&amp;quot;&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  c = Class.new&lt;br /&gt;
  c.class_eval do&lt;br /&gt;
    define_method :hi do&lt;br /&gt;
    puts &amp;quot;Hey&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  c.new.hi&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
This technique allows you to add any type of new code and modify behavior of a program at run time.&lt;br /&gt;
Another example, adding attr_accessor to all classes:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  Object.class_eval do&lt;br /&gt;
    class &amp;lt;&amp;lt; self&lt;br /&gt;
      def attribute_accessor( *instance_variables )  &lt;br /&gt;
        instance_variables.each do |instance_variable| &lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
            def #{instance_variable}&lt;br /&gt;
                @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
                @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ?&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
===The C preprocessor (CPP)&amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/library/l-metaprog1/index.html The Art of Metaprogramming]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
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.&lt;br /&gt;
=====The SWAP macro: =====&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type. A macro is effective in this situation because:&lt;br /&gt;
*A function call would take too much overhead for a simple operation&lt;br /&gt;
*The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers. &lt;br /&gt;
*A different function would need to be coded for each type of item you wanted to swap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  #define SWAP(a, b, type) { type __tmp_c; c = b; b = a; a = c; }&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
      int a = 3;&lt;br /&gt;
      int b = 5;&lt;br /&gt;
      printf(&amp;quot;a is %d and b is %d\n&amp;quot;, a, b);&lt;br /&gt;
      SWAP(a, b, int);&lt;br /&gt;
      printf(&amp;quot;a is now %d and b is now %d\n&amp;quot;, a, b);&lt;br /&gt;
      return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====The MIN macro: =====&lt;br /&gt;
  #define MIN(x, y) ((x) &amp;gt; (y) ? (y) : (x))&lt;br /&gt;
&lt;br /&gt;
This macro returns the the minimum of two values.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73551</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73551"/>
		<updated>2013-02-21T04:03:47Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* Responding to a Non-Existent Method Call */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Create a new class at runtime&lt;br /&gt;
::*Create new methods at runtime&lt;br /&gt;
::*Modify existing classes and/or methods at runtime&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
*Ruby is a [http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing dynamically typed language]&lt;br /&gt;
&lt;br /&gt;
::In dynamically typed languages, the majority of its type checking is performed at run-time, as opposed to compile time.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing Wikipedia Article on the Type System]&amp;lt;/ref&amp;gt;  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.&lt;br /&gt;
&lt;br /&gt;
*Ruby is an [http://en.wikipedia.org/wiki/Interpreted_language interpreted language]&lt;br /&gt;
::Interpreted languages avoid explicit compilation&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Interpreted_language Wikipeida Article on Interpreted Language]&amp;lt;/ref&amp;gt;.  Because Ruby is being interpreted as it is being executed, it is possible to create new classes and methods during program execution.&lt;br /&gt;
&lt;br /&gt;
*Ruby classes are [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open]&lt;br /&gt;
&lt;br /&gt;
::Most object-oriented programming languages adhere to the [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open/closed principle]&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle Wikipedia Article on the Open/Closed Principle]&amp;lt;/ref&amp;gt;.  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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples of Metaprogramming in Ruby ==&lt;br /&gt;
&lt;br /&gt;
=== Creating a New Class at Runtime ===&lt;br /&gt;
Below is an example of creating a new class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         puts &amp;quot;MyClass defined&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In example shown above, the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method is invoked on the &amp;lt;tt&amp;gt;3&amp;lt;/tt&amp;gt; object.  Inside of the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method call, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is defined.  &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is actually defined three times, once for each of the three iterations.  Even though the class is defined three times, it has the same effect as only defining it once.  The important thing to note here, is that a new class is defined at runtime, inside of a method call.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Class at Runtime ===&lt;br /&gt;
Below is an example of modifying a class, and and example of creating a new method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         def one&lt;br /&gt;
            puts &amp;quot;one&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is modified again to include a new method.  This method, the &amp;lt;tt&amp;gt;one&amp;lt;/tt&amp;gt; method is defined three times, just like the class is defined three times.  Even though it has been defined three times, it has the same effect as only defining it once.  The important thing to note here is that a new method is defined at runtime.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Creating a Method at Runtime ===&lt;br /&gt;
Below is an additional example of modifying a class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class&amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is reopened and the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is added to it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Method at Runtime ===&lt;br /&gt;
&lt;br /&gt;
Below is an additional example of modifying a method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;one two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is reopened and modified.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the modified code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   one two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Responding to a Non-Existent Method Call ===&lt;br /&gt;
Whenever a call to an undefined method is made on an object, Ruby provides an option to intercept the call. This is done by implementing the method method_missing within the class definition.  Ruby passes as parameters the name of the method called and the arguments passed to it. Another interesting use of method_missing can be found on the Web by looking for “Roman method_missing”. Define a module (or class) Roman.  This class contains a method_missing method that intercepts calls to “class methods” that are undefined.  It then tries to interpret the method name as a Roman numeral.&lt;br /&gt;
For example, &lt;br /&gt;
*evaluating Roman.xix calls the xix,method of module Roman.  &lt;br /&gt;
*Roman has no xix method, so method_missing is invoked with &lt;br /&gt;
*xix as the argument.  &lt;br /&gt;
*The id2name method of class Symbol is invoked on :xix, returning &amp;quot;xix&amp;quot;. &lt;br /&gt;
*The &amp;quot;xix&amp;quot; is then parsed according to the rules for evaluating Roman numerals, and evaluates to 19.&lt;br /&gt;
&lt;br /&gt;
class Roman &lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,eek 2 Object-Oriented Languages and Systems 23&lt;br /&gt;
  CSC 517 23&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    last = nil&lt;br /&gt;
    &lt;br /&gt;
  roman_string.to_s.upcase.split(//).reverse.inject(0) do&lt;br /&gt;
  |memo, digit|&lt;br /&gt;
      if digit_value = DIGITS[digit]&lt;br /&gt;
        if last &amp;amp;&amp;amp; last &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
  last = digit_value&lt;br /&gt;
      end&lt;br /&gt;
  memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby has several metaprogramming facilities.  One can create new class and add methods to it using &amp;quot;define_method&amp;quot;&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  c = Class.new&lt;br /&gt;
  c.class_eval do&lt;br /&gt;
    define_method :hi do&lt;br /&gt;
    puts &amp;quot;Hey&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  c.new.hi&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
This technique allows you to add any type of new code and modify behavior of a program at run time.&lt;br /&gt;
Another example, adding attr_accessor to all classes:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  Object.class_eval do&lt;br /&gt;
    class &amp;lt;&amp;lt; self&lt;br /&gt;
      def attribute_accessor( *instance_variables )  &lt;br /&gt;
        instance_variables.each do |instance_variable| &lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
            def #{instance_variable}&lt;br /&gt;
                @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
                @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ?&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
===The C preprocessor (CPP)&amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/library/l-metaprog1/index.html The Art of Metaprogramming]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
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.&lt;br /&gt;
=====The SWAP macro: =====&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type. A macro is effective in this situation because:&lt;br /&gt;
*A function call would take too much overhead for a simple operation&lt;br /&gt;
*The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers. &lt;br /&gt;
*A different function would need to be coded for each type of item you wanted to swap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  #define SWAP(a, b, type) { type __tmp_c; c = b; b = a; a = c; }&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
      int a = 3;&lt;br /&gt;
      int b = 5;&lt;br /&gt;
      printf(&amp;quot;a is %d and b is %d\n&amp;quot;, a, b);&lt;br /&gt;
      SWAP(a, b, int);&lt;br /&gt;
      printf(&amp;quot;a is now %d and b is now %d\n&amp;quot;, a, b);&lt;br /&gt;
      return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====The MIN macro: =====&lt;br /&gt;
  #define MIN(x, y) ((x) &amp;gt; (y) ? (y) : (x))&lt;br /&gt;
&lt;br /&gt;
This macro returns the the minimum of two values.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73550</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73550"/>
		<updated>2013-02-21T04:03:28Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* Responding to a Non-Existent Method Call */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Create a new class at runtime&lt;br /&gt;
::*Create new methods at runtime&lt;br /&gt;
::*Modify existing classes and/or methods at runtime&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
*Ruby is a [http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing dynamically typed language]&lt;br /&gt;
&lt;br /&gt;
::In dynamically typed languages, the majority of its type checking is performed at run-time, as opposed to compile time.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing Wikipedia Article on the Type System]&amp;lt;/ref&amp;gt;  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.&lt;br /&gt;
&lt;br /&gt;
*Ruby is an [http://en.wikipedia.org/wiki/Interpreted_language interpreted language]&lt;br /&gt;
::Interpreted languages avoid explicit compilation&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Interpreted_language Wikipeida Article on Interpreted Language]&amp;lt;/ref&amp;gt;.  Because Ruby is being interpreted as it is being executed, it is possible to create new classes and methods during program execution.&lt;br /&gt;
&lt;br /&gt;
*Ruby classes are [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open]&lt;br /&gt;
&lt;br /&gt;
::Most object-oriented programming languages adhere to the [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open/closed principle]&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle Wikipedia Article on the Open/Closed Principle]&amp;lt;/ref&amp;gt;.  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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples of Metaprogramming in Ruby ==&lt;br /&gt;
&lt;br /&gt;
=== Creating a New Class at Runtime ===&lt;br /&gt;
Below is an example of creating a new class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         puts &amp;quot;MyClass defined&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In example shown above, the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method is invoked on the &amp;lt;tt&amp;gt;3&amp;lt;/tt&amp;gt; object.  Inside of the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method call, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is defined.  &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is actually defined three times, once for each of the three iterations.  Even though the class is defined three times, it has the same effect as only defining it once.  The important thing to note here, is that a new class is defined at runtime, inside of a method call.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Class at Runtime ===&lt;br /&gt;
Below is an example of modifying a class, and and example of creating a new method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         def one&lt;br /&gt;
            puts &amp;quot;one&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is modified again to include a new method.  This method, the &amp;lt;tt&amp;gt;one&amp;lt;/tt&amp;gt; method is defined three times, just like the class is defined three times.  Even though it has been defined three times, it has the same effect as only defining it once.  The important thing to note here is that a new method is defined at runtime.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Creating a Method at Runtime ===&lt;br /&gt;
Below is an additional example of modifying a class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class&amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is reopened and the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is added to it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Method at Runtime ===&lt;br /&gt;
&lt;br /&gt;
Below is an additional example of modifying a method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;one two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is reopened and modified.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the modified code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   one two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Responding to a Non-Existent Method Call ===&lt;br /&gt;
=== Responding to a Non-Existent Method Call ===&lt;br /&gt;
Whenever a call to an undefined method is made on an object, Ruby provides an option to intercept the call. This is done by implementing the method method_missing within the class definition.  Ruby passes as parameters the name of the method called and the arguments passed to it. Another interesting use of method_missing can be found on the Web by looking for “Roman method_missing”. Define a module (or class) Roman.  This class contains a method_missing method that intercepts calls to “class methods” that are undefined.  It then tries to interpret the method name as a Roman numeral.&lt;br /&gt;
For example, &lt;br /&gt;
*evaluating Roman.xix calls the xix,method of module Roman.  &lt;br /&gt;
*Roman has no xix method, so method_missing is invoked with &lt;br /&gt;
*xix as the argument.  &lt;br /&gt;
*The id2name method of class Symbol is invoked on :xix, returning &amp;quot;xix&amp;quot;. &lt;br /&gt;
*The &amp;quot;xix&amp;quot; is then parsed according to the rules for evaluating Roman numerals, and evaluates to 19.&lt;br /&gt;
&lt;br /&gt;
class Roman &lt;br /&gt;
  DIGITS = {&lt;br /&gt;
    'I' =&amp;gt; 1,&lt;br /&gt;
    'V' =&amp;gt; 5,&lt;br /&gt;
    'X' =&amp;gt; 10,&lt;br /&gt;
    'L' =&amp;gt; 50,&lt;br /&gt;
    'C' =&amp;gt; 100,eek 2 Object-Oriented Languages and Systems 23&lt;br /&gt;
  CSC 517 23&lt;br /&gt;
    'D' =&amp;gt; 500,&lt;br /&gt;
    'M' =&amp;gt; 1000,&lt;br /&gt;
  }&lt;br /&gt;
  def roman_to_integer(roman_string)&lt;br /&gt;
    last = nil&lt;br /&gt;
    &lt;br /&gt;
  roman_string.to_s.upcase.split(//).reverse.inject(0) do&lt;br /&gt;
  |memo, digit|&lt;br /&gt;
      if digit_value = DIGITS[digit]&lt;br /&gt;
        if last &amp;amp;&amp;amp; last &amp;gt; digit_value&lt;br /&gt;
          memo -= digit_value&lt;br /&gt;
        else&lt;br /&gt;
          memo += digit_value&lt;br /&gt;
        end&lt;br /&gt;
  last = digit_value&lt;br /&gt;
      end&lt;br /&gt;
  memo&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  def method_missing(method)        &lt;br /&gt;
    str = method.id2name &lt;br /&gt;
    roman_to_integer(str)      &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby has several metaprogramming facilities.  One can create new class and add methods to it using &amp;quot;define_method&amp;quot;&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  c = Class.new&lt;br /&gt;
  c.class_eval do&lt;br /&gt;
    define_method :hi do&lt;br /&gt;
    puts &amp;quot;Hey&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  c.new.hi&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
This technique allows you to add any type of new code and modify behavior of a program at run time.&lt;br /&gt;
Another example, adding attr_accessor to all classes:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  Object.class_eval do&lt;br /&gt;
    class &amp;lt;&amp;lt; self&lt;br /&gt;
      def attribute_accessor( *instance_variables )  &lt;br /&gt;
        instance_variables.each do |instance_variable| &lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
            def #{instance_variable}&lt;br /&gt;
                @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
                @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ?&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
===The C preprocessor (CPP)&amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/library/l-metaprog1/index.html The Art of Metaprogramming]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
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.&lt;br /&gt;
=====The SWAP macro: =====&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type. A macro is effective in this situation because:&lt;br /&gt;
*A function call would take too much overhead for a simple operation&lt;br /&gt;
*The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers. &lt;br /&gt;
*A different function would need to be coded for each type of item you wanted to swap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  #define SWAP(a, b, type) { type __tmp_c; c = b; b = a; a = c; }&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
      int a = 3;&lt;br /&gt;
      int b = 5;&lt;br /&gt;
      printf(&amp;quot;a is %d and b is %d\n&amp;quot;, a, b);&lt;br /&gt;
      SWAP(a, b, int);&lt;br /&gt;
      printf(&amp;quot;a is now %d and b is now %d\n&amp;quot;, a, b);&lt;br /&gt;
      return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====The MIN macro: =====&lt;br /&gt;
  #define MIN(x, y) ((x) &amp;gt; (y) ? (y) : (x))&lt;br /&gt;
&lt;br /&gt;
This macro returns the the minimum of two values.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73548</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73548"/>
		<updated>2013-02-21T04:02:47Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Create a new class at runtime&lt;br /&gt;
::*Create new methods at runtime&lt;br /&gt;
::*Modify existing classes and/or methods at runtime&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
*Ruby is a [http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing dynamically typed language]&lt;br /&gt;
&lt;br /&gt;
::In dynamically typed languages, the majority of its type checking is performed at run-time, as opposed to compile time.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing Wikipedia Article on the Type System]&amp;lt;/ref&amp;gt;  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.&lt;br /&gt;
&lt;br /&gt;
*Ruby is an [http://en.wikipedia.org/wiki/Interpreted_language interpreted language]&lt;br /&gt;
::Interpreted languages avoid explicit compilation&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Interpreted_language Wikipeida Article on Interpreted Language]&amp;lt;/ref&amp;gt;.  Because Ruby is being interpreted as it is being executed, it is possible to create new classes and methods during program execution.&lt;br /&gt;
&lt;br /&gt;
*Ruby classes are [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open]&lt;br /&gt;
&lt;br /&gt;
::Most object-oriented programming languages adhere to the [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open/closed principle]&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle Wikipedia Article on the Open/Closed Principle]&amp;lt;/ref&amp;gt;.  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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Examples of Metaprogramming in Ruby ==&lt;br /&gt;
&lt;br /&gt;
=== Creating a New Class at Runtime ===&lt;br /&gt;
Below is an example of creating a new class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         puts &amp;quot;MyClass defined&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In example shown above, the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method is invoked on the &amp;lt;tt&amp;gt;3&amp;lt;/tt&amp;gt; object.  Inside of the &amp;lt;tt&amp;gt;times&amp;lt;/tt&amp;gt; method call, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is defined.  &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is actually defined three times, once for each of the three iterations.  Even though the class is defined three times, it has the same effect as only defining it once.  The important thing to note here, is that a new class is defined at runtime, inside of a method call.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Class at Runtime ===&lt;br /&gt;
Below is an example of modifying a class, and and example of creating a new method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;3.times do&lt;br /&gt;
      class MyClass&lt;br /&gt;
         def one&lt;br /&gt;
            puts &amp;quot;one&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class &amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is modified again to include a new method.  This method, the &amp;lt;tt&amp;gt;one&amp;lt;/tt&amp;gt; method is defined three times, just like the class is defined three times.  Even though it has been defined three times, it has the same effect as only defining it once.  The important thing to note here is that a new method is defined at runtime.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Creating a Method at Runtime ===&lt;br /&gt;
Below is an additional example of modifying a class at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the class&amp;lt;tt&amp;gt;MyClass&amp;lt;/tt&amp;gt; is reopened and the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is added to it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Modifying a Method at Runtime ===&lt;br /&gt;
&lt;br /&gt;
Below is an additional example of modifying a method at runtime:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
    &amp;lt;code&amp;gt;class MyClass&lt;br /&gt;
      def two&lt;br /&gt;
         puts &amp;quot;one two&amp;quot;&lt;br /&gt;
   end&amp;lt;/code&amp;gt;&lt;br /&gt;
In the example shown above, the &amp;lt;tt&amp;gt;two&amp;lt;/tt&amp;gt; method is reopened and modified.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example of what happens when the modified code written above is executed:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
   &amp;lt;code&amp;gt;&lt;br /&gt;
   mc = MyClass.new&lt;br /&gt;
   mc.one&lt;br /&gt;
   mc.two&lt;br /&gt;
   &amp;lt;/code&amp;gt;&lt;br /&gt;
The output of the code will be:&lt;br /&gt;
   &amp;lt;code&amp;gt;one&lt;br /&gt;
   one two&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Responding to a Non-Existent Method Call ===&lt;br /&gt;
&lt;br /&gt;
Ruby has several metaprogramming facilities.  One can create new class and add methods to it using &amp;quot;define_method&amp;quot;&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  c = Class.new&lt;br /&gt;
  c.class_eval do&lt;br /&gt;
    define_method :hi do&lt;br /&gt;
    puts &amp;quot;Hey&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  c.new.hi&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
This technique allows you to add any type of new code and modify behavior of a program at run time.&lt;br /&gt;
Another example, adding attr_accessor to all classes:&amp;lt;ref name=&amp;quot;DrGCodeNotes&amp;quot;&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf Dr. Gehrenger's notes on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
  Object.class_eval do&lt;br /&gt;
    class &amp;lt;&amp;lt; self&lt;br /&gt;
      def attribute_accessor( *instance_variables )  &lt;br /&gt;
        instance_variables.each do |instance_variable| &lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
            def #{instance_variable}&lt;br /&gt;
                @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
                @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ?&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end &lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
===The C preprocessor (CPP)&amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/library/l-metaprog1/index.html The Art of Metaprogramming]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
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.&lt;br /&gt;
=====The SWAP macro: =====&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type. A macro is effective in this situation because:&lt;br /&gt;
*A function call would take too much overhead for a simple operation&lt;br /&gt;
*The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers. &lt;br /&gt;
*A different function would need to be coded for each type of item you wanted to swap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  #define SWAP(a, b, type) { type __tmp_c; c = b; b = a; a = c; }&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
      int a = 3;&lt;br /&gt;
      int b = 5;&lt;br /&gt;
      printf(&amp;quot;a is %d and b is %d\n&amp;quot;, a, b);&lt;br /&gt;
      SWAP(a, b, int);&lt;br /&gt;
      printf(&amp;quot;a is now %d and b is now %d\n&amp;quot;, a, b);&lt;br /&gt;
      return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====The MIN macro: =====&lt;br /&gt;
  #define MIN(x, y) ((x) &amp;gt; (y) ? (y) : (x))&lt;br /&gt;
&lt;br /&gt;
This macro returns the the minimum of two values.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73547</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73547"/>
		<updated>2013-02-21T03:54:48Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* Examples of Metaprogramming in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Create a new class at runtime&lt;br /&gt;
::*Create new methods at runtime&lt;br /&gt;
::*Modify existing classes and/or methods at runtime&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
*Ruby is a [http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing dynamically typed language]&lt;br /&gt;
&lt;br /&gt;
::In dynamically typed languages, the majority of its type checking is performed at run-time, as opposed to compile time.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing Wikipedia Article on the Type System]&amp;lt;/ref&amp;gt;  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.&lt;br /&gt;
&lt;br /&gt;
*Ruby is an [http://en.wikipedia.org/wiki/Interpreted_language interpreted language]&lt;br /&gt;
::Interpreted languages avoid explicit compilation&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Interpreted_language Wikipeida Article on Interpreted Language]&amp;lt;/ref&amp;gt;.  Because Ruby is being interpreted as it is being executed, it is possible to create new classes and methods during program execution.&lt;br /&gt;
&lt;br /&gt;
*Ruby classes are [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open]&lt;br /&gt;
&lt;br /&gt;
::Most object-oriented programming languages adhere to the [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open/closed principle]&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle Wikipedia Article on the Open/Closed Principle]&amp;lt;/ref&amp;gt;.  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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
===The C preprocessor (CPP)&amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/library/l-metaprog1/index.html The Art of Metaprogramming]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
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.&lt;br /&gt;
=====The SWAP macro: =====&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type. A macro is effective in this situation because:&lt;br /&gt;
*A function call would take too much overhead for a simple operation&lt;br /&gt;
*The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers. &lt;br /&gt;
*A different function would need to be coded for each type of item you wanted to swap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  #define SWAP(a, b, type) { type __tmp_c; c = b; b = a; a = c; }&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
      int a = 3;&lt;br /&gt;
      int b = 5;&lt;br /&gt;
      printf(&amp;quot;a is %d and b is %d\n&amp;quot;, a, b);&lt;br /&gt;
      SWAP(a, b, int);&lt;br /&gt;
      printf(&amp;quot;a is now %d and b is now %d\n&amp;quot;, a, b);&lt;br /&gt;
      return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====The MIN macro: =====&lt;br /&gt;
  #define MIN(x, y) ((x) &amp;gt; (y) ? (y) : (x))&lt;br /&gt;
&lt;br /&gt;
This macro returns the the minimum of two values.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73337</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73337"/>
		<updated>2013-02-20T19:12:04Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Add methods to objects at runtime&lt;br /&gt;
::*Determine if an oject responds to a message or contains a property&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
::*Respond to queries made on non-existent properties &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
*Ruby is a [http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing dynamically typed language]&lt;br /&gt;
&lt;br /&gt;
::In dynamically typed languages, the majority of its type checking is performed at run-time, as opposed to compile time.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing Wikipedia Article on the Type System]&amp;lt;/ref&amp;gt;  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.&lt;br /&gt;
&lt;br /&gt;
*Ruby is an [http://en.wikipedia.org/wiki/Interpreted_language interpreted language]&lt;br /&gt;
::Interpreted languages avoid explicit compilation&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Interpreted_language Wikipeida Article on Interpreted Language]&amp;lt;/ref&amp;gt;.  Because Ruby is being interpreted as it is being executed, it is possible to create new classes and methods during program execution.&lt;br /&gt;
&lt;br /&gt;
*Ruby classes are [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open]&lt;br /&gt;
&lt;br /&gt;
::Most object-oriented programming languages adhere to the [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open/closed principle]&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle Wikipedia Article on the Open/Closed Principle]&amp;lt;/ref&amp;gt;.  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.&lt;br /&gt;
== Examples of Metaprogramming in Ruby &amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/lectures/notes/ol5.pdf]Dr. Gehrenger's notes on Metaprogramming&amp;lt;/ref&amp;gt;==&lt;br /&gt;
Ruby has several metaprogramming facilities.  One can create new class and add methods to it using &amp;quot;define_method&amp;quot;&lt;br /&gt;
  c = Class.new&lt;br /&gt;
  c.class_eval do&lt;br /&gt;
    define_method :hi do&lt;br /&gt;
    puts &amp;quot;Hey&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  c.new.hi&lt;br /&gt;
  &amp;gt;&amp;gt;Hey&lt;br /&gt;
&lt;br /&gt;
This technique allows you to add any type of new code and modify behavior of a program at run time.&lt;br /&gt;
Another example, adding attr_accessor to all classes:&lt;br /&gt;
  Object.class_eval do&lt;br /&gt;
    class &amp;lt;&amp;lt; self&lt;br /&gt;
      def attribute_accessor( *instance_variables )  &lt;br /&gt;
        instance_variables.each do |instance_variable| &lt;br /&gt;
          class_eval %Q?&lt;br /&gt;
            def #{instance_variable}&lt;br /&gt;
                @#{instance_variable}&lt;br /&gt;
            end&lt;br /&gt;
            def #{instance_variable}=( new_value )&lt;br /&gt;
                @#{instance_variable} = new_value&lt;br /&gt;
            end&lt;br /&gt;
          ?&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end &lt;br /&gt;
  end &lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
===The C preprocessor (CPP)&amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/library/l-metaprog1/index.html The Art of Metaprogramming]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
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.&lt;br /&gt;
=====The SWAP macro: =====&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type. A macro is effective in this situation because:&lt;br /&gt;
*A function call would take too much overhead for a simple operation&lt;br /&gt;
*The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers. &lt;br /&gt;
*A different function would need to be coded for each type of item you wanted to swap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  #define SWAP(a, b, type) { type __tmp_c; c = b; b = a; a = c; }&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
      int a = 3;&lt;br /&gt;
      int b = 5;&lt;br /&gt;
      printf(&amp;quot;a is %d and b is %d\n&amp;quot;, a, b);&lt;br /&gt;
      SWAP(a, b, int);&lt;br /&gt;
      printf(&amp;quot;a is now %d and b is now %d\n&amp;quot;, a, b);&lt;br /&gt;
      return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====The MIN macro: =====&lt;br /&gt;
  #define MIN(x, y) ((x) &amp;gt; (y) ? (y) : (x))&lt;br /&gt;
&lt;br /&gt;
This macro returns the the minimum of two values.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73336</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73336"/>
		<updated>2013-02-20T19:05:13Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* Examples of Metaprogramming in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Add methods to objects at runtime&lt;br /&gt;
::*Determine if an oject responds to a message or contains a property&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
::*Respond to queries made on non-existent properties &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
*Ruby is a [http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing dynamically typed language]&lt;br /&gt;
&lt;br /&gt;
::In dynamically typed languages, the majority of its type checking is performed at run-time, as opposed to compile time.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing Wikipedia Article on the Type System]&amp;lt;/ref&amp;gt;  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.&lt;br /&gt;
&lt;br /&gt;
*Ruby is an [http://en.wikipedia.org/wiki/Interpreted_language interpreted language]&lt;br /&gt;
::Interpreted languages avoid explicit compilation&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Interpreted_language Wikipeida Article on Interpreted Language]&amp;lt;/ref&amp;gt;.  Because Ruby is being interpreted as it is being executed, it is possible to create new classes and methods during program execution.&lt;br /&gt;
&lt;br /&gt;
*Ruby classes are [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open]&lt;br /&gt;
&lt;br /&gt;
::Most object-oriented programming languages adhere to the [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open/closed principle]&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle Wikipedia Article on the Open/Closed Principle]&amp;lt;/ref&amp;gt;.  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.&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
===The C preprocessor (CPP)&amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/library/l-metaprog1/index.html The Art of Metaprogramming]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
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.&lt;br /&gt;
=====The SWAP macro: =====&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type. A macro is effective in this situation because:&lt;br /&gt;
*A function call would take too much overhead for a simple operation&lt;br /&gt;
*The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers. &lt;br /&gt;
*A different function would need to be coded for each type of item you wanted to swap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  #define SWAP(a, b, type) { type __tmp_c; c = b; b = a; a = c; }&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
      int a = 3;&lt;br /&gt;
      int b = 5;&lt;br /&gt;
      printf(&amp;quot;a is %d and b is %d\n&amp;quot;, a, b);&lt;br /&gt;
      SWAP(a, b, int);&lt;br /&gt;
      printf(&amp;quot;a is now %d and b is now %d\n&amp;quot;, a, b);&lt;br /&gt;
      return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====The MIN macro: =====&lt;br /&gt;
  #define MIN(x, y) ((x) &amp;gt; (y) ? (y) : (x))&lt;br /&gt;
&lt;br /&gt;
This macro returns the the minimum of two values.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73317</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73317"/>
		<updated>2013-02-20T18:19:30Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* Metaprogramming in Other Languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Add methods to objects at runtime&lt;br /&gt;
::*Determine if an oject responds to a message or contains a property&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
::*Respond to queries made on non-existent properties &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
*Ruby is a [http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing dynamically typed language]&lt;br /&gt;
&lt;br /&gt;
::In dynamically typed languages, the majority of its type checking is performed at run-time, as opposed to compile time.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing Wikipedia Article on the Type System]&amp;lt;/ref&amp;gt;  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.&lt;br /&gt;
&lt;br /&gt;
*Ruby is an [http://en.wikipedia.org/wiki/Interpreted_language interpreted language]&lt;br /&gt;
::Interpreted languages avoid explicit compilation&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Interpreted_language Wikipeida Article on Interpreted Language]&amp;lt;/ref&amp;gt;.  Because Ruby is being interpreted as it is being executed, it is possible to create new classes and methods during program execution.&lt;br /&gt;
&lt;br /&gt;
*Ruby classes are [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open]&lt;br /&gt;
&lt;br /&gt;
::Most object-oriented programming languages adhere to the [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open/closed principle]&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle Wikipedia Article on the Open/Closed Principle]&amp;lt;/ref&amp;gt;.  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.&lt;br /&gt;
&lt;br /&gt;
== Examples of Metaprogramming in Ruby ==&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
===The C preprocessor (CPP)&amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/library/l-metaprog1/index.html The Art of Metaprogramming]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
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.&lt;br /&gt;
=====The SWAP macro: =====&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type. A macro is effective in this situation because:&lt;br /&gt;
*A function call would take too much overhead for a simple operation&lt;br /&gt;
*The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers. &lt;br /&gt;
*A different function would need to be coded for each type of item you wanted to swap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  #define SWAP(a, b, type) { type __tmp_c; c = b; b = a; a = c; }&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
      int a = 3;&lt;br /&gt;
      int b = 5;&lt;br /&gt;
      printf(&amp;quot;a is %d and b is %d\n&amp;quot;, a, b);&lt;br /&gt;
      SWAP(a, b, int);&lt;br /&gt;
      printf(&amp;quot;a is now %d and b is now %d\n&amp;quot;, a, b);&lt;br /&gt;
      return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====The MIN macro: =====&lt;br /&gt;
  #define MIN(x, y) ((x) &amp;gt; (y) ? (y) : (x))&lt;br /&gt;
&lt;br /&gt;
This macro returns the the minimum of two values.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73316</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73316"/>
		<updated>2013-02-20T18:18:43Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Add methods to objects at runtime&lt;br /&gt;
::*Determine if an oject responds to a message or contains a property&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
::*Respond to queries made on non-existent properties &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
*Ruby is a [http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing dynamically typed language]&lt;br /&gt;
&lt;br /&gt;
::In dynamically typed languages, the majority of its type checking is performed at run-time, as opposed to compile time.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing Wikipedia Article on the Type System]&amp;lt;/ref&amp;gt;  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.&lt;br /&gt;
&lt;br /&gt;
*Ruby is an [http://en.wikipedia.org/wiki/Interpreted_language interpreted language]&lt;br /&gt;
::Interpreted languages avoid explicit compilation&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Interpreted_language Wikipeida Article on Interpreted Language]&amp;lt;/ref&amp;gt;.  Because Ruby is being interpreted as it is being executed, it is possible to create new classes and methods during program execution.&lt;br /&gt;
&lt;br /&gt;
*Ruby classes are [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open]&lt;br /&gt;
&lt;br /&gt;
::Most object-oriented programming languages adhere to the [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open/closed principle]&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle Wikipedia Article on the Open/Closed Principle]&amp;lt;/ref&amp;gt;.  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.&lt;br /&gt;
&lt;br /&gt;
== Examples of Metaprogramming in Ruby ==&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
::* [http://www.ibm.com/developerworks/library/l-metaprog1/index.html] - The art of metaprogramming&lt;br /&gt;
===The C preprocessor (CPP)&amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/library/l-metaprog1/index.html The art of metaprogramming]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
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.&lt;br /&gt;
=====The SWAP macro: =====&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type. A macro is effective in this situation because:&lt;br /&gt;
*A function call would take too much overhead for a simple operation&lt;br /&gt;
*The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers. &lt;br /&gt;
*A different function would need to be coded for each type of item you wanted to swap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  #define SWAP(a, b, type) { type __tmp_c; c = b; b = a; a = c; }&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
      int a = 3;&lt;br /&gt;
      int b = 5;&lt;br /&gt;
      printf(&amp;quot;a is %d and b is %d\n&amp;quot;, a, b);&lt;br /&gt;
      SWAP(a, b, int);&lt;br /&gt;
      printf(&amp;quot;a is now %d and b is now %d\n&amp;quot;, a, b);&lt;br /&gt;
      return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====The MIN macro: =====&lt;br /&gt;
  #define MIN(x, y) ((x) &amp;gt; (y) ? (y) : (x))&lt;br /&gt;
&lt;br /&gt;
This macro returns the the minimum of two values.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73315</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73315"/>
		<updated>2013-02-20T18:17:01Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Add methods to objects at runtime&lt;br /&gt;
::*Determine if an oject responds to a message or contains a property&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
::*Respond to queries made on non-existent properties &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
*Ruby is a [http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing dynamically typed language]&lt;br /&gt;
&lt;br /&gt;
::In dynamically typed languages, the majority of its type checking is performed at run-time, as opposed to compile time.&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing Wikipedia Article on the Type System]&amp;lt;/ref&amp;gt;  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.&lt;br /&gt;
&lt;br /&gt;
*Ruby is an [http://en.wikipedia.org/wiki/Interpreted_language interpreted language]&lt;br /&gt;
::Interpreted languages avoid explicit compilation&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Interpreted_language Wikipeida Article on Interpreted Language]&amp;lt;/ref&amp;gt;.  Because Ruby is being interpreted as it is being executed, it is possible to create new classes and methods during program execution.&lt;br /&gt;
&lt;br /&gt;
*Ruby classes are [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open]&lt;br /&gt;
&lt;br /&gt;
::Most object-oriented programming languages adhere to the [http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle open/closed principle]&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Open/closed_principle#Meyer.27s_Open.2FClosed_Principle Wikipedia Article on the Open/Closed Principle]&amp;lt;/ref&amp;gt;.  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.&lt;br /&gt;
&lt;br /&gt;
== Examples of Metaprogramming in Ruby ==&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
::* [http://www.ibm.com/developerworks/library/l-metaprog1/index.html] - The art of metaprogramming&lt;br /&gt;
===The C preprocessor (CPP)&amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/library/l-metaprog1/index.html]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
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.&lt;br /&gt;
=====The SWAP macro: =====&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type. A macro is effective in this situation because:&lt;br /&gt;
*A function call would take too much overhead for a simple operation&lt;br /&gt;
*The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers. &lt;br /&gt;
*A different function would need to be coded for each type of item you wanted to swap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  #define SWAP(a, b, type) { type __tmp_c; c = b; b = a; a = c; }&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
      int a = 3;&lt;br /&gt;
      int b = 5;&lt;br /&gt;
      printf(&amp;quot;a is %d and b is %d\n&amp;quot;, a, b);&lt;br /&gt;
      SWAP(a, b, int);&lt;br /&gt;
      printf(&amp;quot;a is now %d and b is now %d\n&amp;quot;, a, b);&lt;br /&gt;
      return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====The MIN macro: =====&lt;br /&gt;
  #define MIN(x, y) ((x) &amp;gt; (y) ? (y) : (x))&lt;br /&gt;
&lt;br /&gt;
This macro returns the the minimum of two values.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73296</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73296"/>
		<updated>2013-02-20T04:10:19Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* Metaprogramming in Other Languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Add methods to objects at runtime&lt;br /&gt;
::*Determine if an oject responds to a message or contains a property&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
::*Respond to queries made on non-existent properties &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
== Examples of Metaprogramming in Ruby ==&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
===The C preprocessor (CPP)===&lt;br /&gt;
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.&lt;br /&gt;
=====The SWAP macro: =====&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type. A macro is effective in this situation because:&lt;br /&gt;
*A function call would take too much overhead for a simple operation&lt;br /&gt;
*The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers. &lt;br /&gt;
*A different function would need to be coded for each type of item you wanted to swap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  #define SWAP(a, b, type) { type __tmp_c; c = b; b = a; a = c; }&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
      int a = 3;&lt;br /&gt;
      int b = 5;&lt;br /&gt;
      printf(&amp;quot;a is %d and b is %d\n&amp;quot;, a, b);&lt;br /&gt;
      SWAP(a, b, int);&lt;br /&gt;
      printf(&amp;quot;a is now %d and b is now %d\n&amp;quot;, a, b);&lt;br /&gt;
      return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====The MIN macro: =====&lt;br /&gt;
  #define MIN(x, y) ((x) &amp;gt; (y) ? (y) : (x))&lt;br /&gt;
&lt;br /&gt;
This macro returns the the minimum of two values.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73288</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73288"/>
		<updated>2013-02-20T03:46:16Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* Metaprogramming in Other Languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Add methods to objects at runtime&lt;br /&gt;
::*Determine if an oject responds to a message or contains a property&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
::*Respond to queries made on non-existent properties &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
== Examples of Metaprogramming in Ruby ==&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
===The C preprocessor (CPP)===&lt;br /&gt;
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.&lt;br /&gt;
=====The SWAP macro: =====&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type. A macro is effective in this situation because:&lt;br /&gt;
*A function call would take too much overhead for a simple operation&lt;br /&gt;
*The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers. &lt;br /&gt;
*A different function would need to be coded for each type of item you wanted to swap.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  #define SWAP(a, b, type) { type __tmp_c; c = b; b = a; a = c; }&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
      int a = 3;&lt;br /&gt;
      int b = 5;&lt;br /&gt;
      printf(&amp;quot;a is %d and b is %d\n&amp;quot;, a, b);&lt;br /&gt;
      SWAP(a, b, int);&lt;br /&gt;
      printf(&amp;quot;a is now %d and b is now %d\n&amp;quot;, a, b);&lt;br /&gt;
      return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73287</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73287"/>
		<updated>2013-02-20T03:40:15Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Add methods to objects at runtime&lt;br /&gt;
::*Determine if an oject responds to a message or contains a property&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
::*Respond to queries made on non-existent properties &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
== Examples of Metaprogramming in Ruby ==&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
===The C preprocessor (CPP)===&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type. A macro is effective in this situation because:&lt;br /&gt;
*A function call would take too much overhead for a simple operation&lt;br /&gt;
*The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers. &lt;br /&gt;
*A different function would need to be coded for each type of item you wanted to swap.&lt;br /&gt;
&lt;br /&gt;
=====Using the SWAP macro: =====&lt;br /&gt;
  #define SWAP(a, b, type) { type __tmp_c; c = b; b = a; a = c; }&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
      int a = 3;&lt;br /&gt;
      int b = 5;&lt;br /&gt;
      printf(&amp;quot;a is %d and b is %d\n&amp;quot;, a, b);&lt;br /&gt;
      SWAP(a, b, int);&lt;br /&gt;
      printf(&amp;quot;a is now %d and b is now %d\n&amp;quot;, a, b);&lt;br /&gt;
      return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73286</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73286"/>
		<updated>2013-02-20T03:32:30Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* The C preprocessor (CPP) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Add methods to objects at runtime&lt;br /&gt;
::*Determine if an oject responds to a message or contains a property&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
::*Respond to queries made on non-existent properties &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
== Examples of Metaprogramming in Ruby ==&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
===The C preprocessor (CPP)===&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type. A macro is effective in this situation because:&lt;br /&gt;
&lt;br /&gt;
*A function call would take too much overhead for a simple operation&lt;br /&gt;
*The functions would require variables' addresses to be passed which is messier and prevents the compiler from keeping the values in registers. &lt;br /&gt;
*A different function would need to be coded for each type of item you wanted to swap.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73285</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73285"/>
		<updated>2013-02-20T03:23:03Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* The C preprocessor (CPP) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Add methods to objects at runtime&lt;br /&gt;
::*Determine if an oject responds to a message or contains a property&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
::*Respond to queries made on non-existent properties &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
== Examples of Metaprogramming in Ruby ==&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
===The C preprocessor (CPP)===&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73284</id>
		<title>CSC/ECE 517 Spring 2013/ch1b 1k hf</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1b_1k_hf&amp;diff=73284"/>
		<updated>2013-02-20T03:22:31Z</updated>

		<summary type="html">&lt;p&gt;Twfolsom: /* Metaprogramming in Other Languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic.&lt;br /&gt;
*Here is a [[CSC/ECE_517_Fall_2011/ch4_4g_as|link]] to previous work on this topic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Basic Definition of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
The most basic definition of metaprogramming is: ''writing code that writes or manipulates code.''&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://courses.ncsu.edu/csc517/common/media/08/Metaprogramming/Metaprogramming.html Video Lecture on Metaprogramming by Dr. Gehringer]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A metaprogram can either manipulate itself, or it can manipulate some other program.&lt;br /&gt;
&lt;br /&gt;
:Below are three ''important terms'' associated with a metaprogramming&amp;lt;ref name=&amp;quot;wiki metaprogramming&amp;quot;&amp;gt;[http://en.wikipedia.org/wiki/Metaprogramming Wikipedia Article on Metaprogramming]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Metalanguage metalanguage] - the language in which a metaprogram is written&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Object_language object language] - the language of the program that is being manipulated by a metaprogram&lt;br /&gt;
::* [http://en.wikipedia.org/wiki/Reflection_(computer_science) reflection] - the ability of a programming language to be its own metalanguage (also known as ''reflexivity'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Uses of Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can allow programmers to write code that is more&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
:One of the best examples of metaprogramming is that of the [http://en.wikipedia.org/wiki/Compiler compiler]&amp;lt;ref&amp;gt;[http://c2.com/cgi/wiki?MetaProgramming Overview of Metaprogramming]&amp;lt;/ref&amp;gt;.  A compiler uses code written in one language in order to generate code that can be executed by a computer.&lt;br /&gt;
&lt;br /&gt;
:Metaprogramming is also used for building frameworks such as [http://rubyonrails.org/ Ruby on Rails].  In this case, the use of [http://en.wikipedia.org/wiki/Convention_over_configuration 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 [http://en.wikipedia.org/wiki/Scaffold_(programming) scaffold] uses the code the code written by the developer to generate code that is expansive, interconnected, and standardized. &lt;br /&gt;
&lt;br /&gt;
:One of the most powerful uses for metaprogramming, is the ability to change a program during runtime.  In languages such as Ruby and [http://groovy.codehaus.org/ Groovy], programmers can&amp;lt;ref&amp;gt;[http://blog.adaptivesoftware.biz/2009/02/what-is-metaprogramming.html Coding Insights Blog]&amp;lt;/ref&amp;gt;:&lt;br /&gt;
::*Add methods to objects at runtime&lt;br /&gt;
::*Determine if an oject responds to a message or contains a property&lt;br /&gt;
::*Respond to calls made on non-existent methods&lt;br /&gt;
::*Respond to queries made on non-existent properties &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Features in Ruby that Enable Metaprogramming ==&lt;br /&gt;
&lt;br /&gt;
== Examples of Metaprogramming in Ruby ==&lt;br /&gt;
&lt;br /&gt;
== Metaprogramming in Other Languages ==&lt;br /&gt;
=The C preprocessor (CPP)=&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
   #define SWAP(a, b, type) { &lt;br /&gt;
       type __tmp_c; c = b; b = a; a = c; &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
This macro allows you to swap the two values of the given type.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Twfolsom</name></author>
	</entry>
</feed>