The Ring programming language version 1.10 book - Part 41 of 212
1. Ring Documentation, Release 1.10
How we can get access to our class attributes and methods while we are inside braces?
This can be done using This.Attribute and This.Method()
Example:
new point
class point
x=10 y=20 z=30
print()
func print
new UI {
display(this.x,this.y,this.z)
}
Class UI
func display x,y,z
see x + nl + y + nl + z + nl
45.15 Using This in the class region as Self
The class region is the region that comes after the class name and before any method.
We can use This in the class region as Self.
Example:
func main
o1 = new program {
test()
}
? o1
class program
this.name = "My Application"
this.version = "1.0"
? name ? version
func test
? "Name = " + name
? "Version = " + version
Output
My Application
1.0
Name = My Application
Version = 1.0
name: My Application
version: 1.0
Note: When we use braces to change the current active object, Using This we can still point to the class.
45.15. Using This in the class region as Self 368
2. Ring Documentation, Release 1.10
Tip: The difference between This and Self is that Self point to the current active object that we can change using
braces.
Remember that in most cases we don’t need to use This or Self in the class region
We can write
class program name version
Or
class program name="My Application" version="1.0"
Note: We use This or Self in the class region just to avoid conflict with global variables that are defined with the same
name.
45.16 Default value for object attributes
The default value for object attributes is NULL
In Ring, the NULL value is just an empty string or a string that contains “NULL”
We can check for NULL values using the isNULL() function
Example:
oProgram = new Program
? oProgram.name
? oProgram.version
? isNULL(oProgram.name)
? isNULL(oProgram.version)
oProgram { name="My Application" version="1.0" }
? isNULL(oProgram.name)
? isNULL(oProgram.version)
? oProgram
class program
name
version
Output:
NULL
NULL
1
1
0
0
name: My Application
version: 1.0
45.16. Default value for object attributes 369
3. CHAPTER
FORTYSIX
FUNCTIONAL PROGRAMMING
In previous chapters we learned about Functions and Recursion.
In this chapter we are going to learn about more Functional Programming (FP) concepts like
• Pure Functions
• First-class functions
• Higher-order functions
• Anonymous and nested functions.
• Equality of functions
46.1 Pure Functions
We can create pure functions (functions that doesn’t change the state) by the help of the assignment operator to copy
variables (Lists & Objects) by value to create new variables instead of working on the original data that are passed to
the function by reference.
Example:
Func Main
aList = [1,2,3,4,5]
aList2 = square(aList)
see "aList" + nl
see aList
see "aList2" + nl
see aList2
Func Square aPara
a1 = aPara # copy the list
for x in a1
x *= x
next
return a1 # return new list
Output:
aList
1
2
3
4
5
370
4. Ring Documentation, Release 1.10
aList2
1
4
9
16
25
46.2 First-class Functions
Functions inside the Ring programming language are first-class citizens, you can pass functions as parameters, return
them as value or store them in variables.
We can pass/return the function by typing the function name as literal like “FunctionName” or :FunctionName for
example.
We can pass/return functions using the variable that contains the function name.
We can call function from variables contains the function name using the Call command
Syntax:
Call Variable([Parameters])
Example:
Func Main
see "before test2()" + nl
f = Test2(:Test)
see "after test2()" + nl
call f()
Func Test
see "Message from test!" + nl
Func Test2 f1
call f1()
See "Message from test2!" + nl
return f1
Output:
before test2()
Message from test!
Message from test2!
after test2()
Message from test!
46.3 Higher-order Functions
Higher-order functions are the functions that takes other functions as parameters.
Example:
Func Main
times(5,:test)
46.2. First-class Functions 371
5. Ring Documentation, Release 1.10
Func Test
see "Message from the test function!" + nl
Func Times nCount,F
for x = 1 to nCount
Call F()
next
Output:
Message from the test function!
Message from the test function!
Message from the test function!
Message from the test function!
Message from the test function!
46.4 Anonymous and Nested Functions
Anonymous Functions are functions without names that can be passed as parameters to other functions or stored in
variables.
Syntax:
Func [Parameters] { [statements] }
Example:
test( func x,y {
see "hello" + nl
see "Sum : " + (x+y) + nl
} )
new great { f1() }
times(3, func { see "hello world" + nl } )
func test x
call x(3,3)
see "wow!" + nl
func times n,x
for t=1 to n
call x()
next
Class great
func f1
f2( func { see "Message from f1" + nl } )
func f2 x
call x()
Output:
hello
Sum : 6
46.4. Anonymous and Nested Functions 372
6. Ring Documentation, Release 1.10
wow!
Message from f1
hello world
hello world
hello world
Example:
Func Main
aList = [1,2,3,4]
Map (aList , func x {
return x*x
} )
see aList
aList = [4,9,14,25]
Map(aList, :myfilter )
see aList
aList = [11,12,13,14]
Map (aList , func x {
if x%2=0
return "even"
else
return "odd"
ok
})
see aList
Func myfilter x
if x = 9
return "True"
else
return "False"
ok
Func Map aList,cFunc
for x in aList
x = call cFunc(x)
next
Output:
1
4
9
16
False
True
False
False
odd
even
odd
even
46.5 Equality of functions
We can test if function = function or not using the ‘=’ or ‘!=’ operators
46.5. Equality of functions 373
7. Ring Documentation, Release 1.10
Example:
f1 = func { see "hello" + nl }
f2 = func { see "how are you?" + nl }
f3 = f1
call f1()
call f2()
call f3()
see (f1 = f2) + nl
see (f2 = f3) + nl
see (f1 = f3) + nl
Output:
hello
how are you?
hello
0
0
1
46.5. Equality of functions 374
8. CHAPTER
FORTYSEVEN
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()
• globals()
• functions()
• cfunctions()
• islocal()
• isglobal()
• isfunction()
• iscfunction()
• packages()
• ispackage()
• classes()
• isclass()
• packageclasses()
• ispackageclass()
• classname()
• objectid()
• isobject()
• attributes()
• methods()
• isattribute()
• isprivateattribute()
• ismethod()
• isprivatemethod()
• addattribute()
• addmethod()
375
9. Ring Documentation, Release 1.10
• getattribute()
• setattribute()
• mergemethods()
• packagename()
47.1 locals() Function
We can get a list of variables names in the current scope using the locals() function.
Syntax:
locals() --> a list contains the variables names in the current scope
Example:
test("hello")
func test cMsg
see cMsg + nl
x = 10
y = 20
z = 30
see locals()
Output:
hello
cmsg
x
y
z
47.2 globals() Function
We can get a list of variables names in the global scope using the globals() function.
Syntax:
globals() --> a list contains variables names in the global scope
Example:
x=10 y=20 z=30
test()
func test
see "message from test()" + nl +
"Global Variables:" + nl
see globals()
Output:
47.1. locals() Function 376
10. Ring Documentation, Release 1.10
message from test()
Global Variables:
x
y
z
47.3 functions() Function
We can get a list of functions names written in the Ring language using the functions() function.
Syntax:
functions() --> a list contains functions names
Example:
see functions()
func f1
see "f1" + nl
func f2
see "f2" + nl
func f3
see "f3" + nl
Output:
f1
f2
f3
47.4 cfunctions() Function
We can get a list of functions names written in the C language using the cfunctions() function.
Syntax:
cfunctions() --> a list contains functions names
Example:
aList = cfunctions()
See "Count : " + len(aList) + nl
for x in aList
see x + "()" + nl
next
Output:
Count : 208
len()
add()
del()
get()
47.3. functions() Function 377