Ring/Lessons/Reflection and Meta-programming

Reflection and Meta-programming
Since the Ring programming language is a dynamic language, we can get answers about the program code and we can modify our code during the runtime.

In this chapter we will learn about this and the available functions to use.

locals Function
We can get a list of variables names in the current scope using the locals function.

Syntax:

Example:

Output:

globals Function
We can get a list of variables names in the global scope using the globals function.

Syntax:

Example:

Output:

functions Function
We can get a list of functions names written in the Ring language using the functions function.

Syntax:

Example:

Output:

cfunctions Function
We can get a list of functions names written in the C language using the cfunctions function.

Syntax:

Example:

Output:

.. note:: The complete list is removed from the previous output.

islocal Function
We can check if a variable is defined in the local scope or not using the islocal function.

Syntax:

Example:

Output:

isglobal Function
We can check if a variable is defined in the global scope or not using the isglobal function.

Syntax:

Example:

Output:

isfunction Function
We can check if a Ring function is defined or not using the isfunction function.

Syntax:

Example:

Output:

iscfunction Function
We can check if a C function is defined or not using the iscfunction function.

Syntax:

Example:

Output:

packages Function
We can get a list of packages names using the packages function.

Syntax:

Example:

Output:

ispackage Function
We can check if a package is defined or not using the ispackage function.

Syntax:

Example:

Output:

classes Function
We can get a list of classes names using the classes function.

Syntax:

Example:

Output:

isclass Function
We can check if a class is defined or not using the isclass function.

Syntax:

Example:

Output:

packageclasses Function
We can get a list of classes names inside a package using the packageclasses function.

Syntax:

Example:

Output:

ispackageclass Function
We can check if a class is defined inside package or not using the ispackageclass function.

Syntax:

Example:

Output:

classname Function
We can know the class name of an object using the classname function

Syntax:

Example:

objectid Function
We can know the object id using the objectid function

Syntax:

Example:

Output:

We can check the variable to know if it's an object or not using the isobject function

Syntax:

attributes Function
We can get the object attributes using the attributes function

Syntax:

Example:

We can get the object methods using the methods function

Syntax:

Example:

Output:

isattribute Function
We can test if the object contains an attribute or not using the isattribute function

Syntax:

Example:

isprivateattribute Function
We can test if the object contains a private attribute or not using the isprivateattribute function

Syntax:

Example:

Output:

ismethod Function
We can test if the object class contains a method or not using the ismethod function

Syntax:

Example:

isprivatemethod Function
We can test if the object class contains a private method or not using the isprivatemethod function

Syntax:

Example:

Output:

addattribute Function
We can add an attribute (or a group of attributes) to the object state (not the class) using the addattribute function

Syntax:

Example(1):

Example(2):

Output:

addmethod Function
We can add a method to the object class using the addmethod function This method can be used with any object from the same class.

Syntax:

Example:

Output:

Instead of using anonymous function to add new method to the class, we can use the function name

Example:

Output:

Since we add the method to the class, any object from that class can use this method

Example:

Output:

getattribute function
We can get the object attribute value using the getattribute function

Syntax:

Example:

Output:

setattribute function
We can set the object attribute value using the setattribute function

Syntax:

Example:

Output:

mergemethods Function
We can share methods between classes without inheritance using the MergeMethods function

This function merge class methods to another class.

Syntax:

Example:

Output: