SlideShare a Scribd company logo
Ring Documentation, Release 1.8
Example:
o1 = new person {
name = "Test"
age = 20
print()
o1.printsalary()
}
try
see o1.salary
catch
see cCatchError + nl
done
try
o1.increasesalary(1000)
catch
see cCatchError + nl
done
Class Person
name age
func print
see "Name : " + name + nl +
"Age : " + age + nl
func printsalary
see "Salary : " + salary + nl
private
salary = 15000
func increasesalary x
salary += x
Output:
Name : Test
Age : 20
Salary : 15000
Error (R27) : Using private attribute from outside the class : salary
Error (R26) : Calling private method from outside the class : increasesalary
42.6 Operator Overloading
We can add the operator method to our class to enable using operators with the class objects.
Syntax:
Class ClassName
...
42.6. Operator Overloading 321
Ring Documentation, Release 1.8
Func operator cOperator,Para
...
The function operator takes two paramters, the first represent the operator and the second represent the second param-
eter after the operator.
Example:
o1 = new point { x = 10 y = 10 print("P1 : ") }
o2 = new point { x = 20 y = 40 print("P2 : ") }
o3 = o1 + o2
o3.print("P1+P2 : ")
class point x y
func operator cOperator,Para
result = new point
switch cOperator
on "+"
result.x = x + Para.x
result.y = y + Para.y
on "-"
result.x = x - Para.x
result.y = y - Para.y
off
return result
func print cPoint
see cPoint + "X : " + x + " Y : " + y + nl
Output:
P1 : X : 10 Y : 10
P2 : X : 20 Y : 40
P1+P2 : X : 30 Y : 50
The next example from the List class in the stdlib.ring
Func operator cOperator,Para
result = new list
switch cOperator
on "+"
if isobject(para)
for t in Para.vValue
vValue + t
next
but islist(para)
for t in Para
vValue + t
next
ok
on "len"
return len( vValue )
on "[]"
return &vValue[para]
off
return result
42.6. Operator Overloading 322
Ring Documentation, Release 1.8
The “len” operator is used with (for in) control structure.
The “[]” operator is used when we try to access the list items, In this case we use the & operator to return the item
values like strings an numbers by reference, so we can update it when we access the items.
42.7 Inheritance
We can create class from another class in the class definition using the keyword from.
Syntax:
Class <Class Name> [From <Parent Class Name>]
We can call a method in the parent class from the child class using the super object.
Syntax:
func methodname
...
super.methodname()
...
Example:
Func main
e1 = new Employee {
Name = "test"
age = 20
job = "programmer"
salary = 20000000
print()
}
Class Human
Name Age
func print
see "Name : " + name + nl + "Age : " + age + nl
Class Employee from Human
Job Salary
func print
super.print()
see "Job : " + job + nl + "Salary : " + salary + nl
Output:
Name : test
Age : 20
Job : programmer
Salary : 20000000
42.8 Dynamic Attributes
We can write instructions after the class name to be executed when we create new objects
Example:
42.7. Inheritance 323
Ring Documentation, Release 1.8
o1 = new dynamicClass
see o1.var5 + nl # output 5
Class DynamicClass
for x = 1 to 10
cStr = "var" + x + " = " + x
eval(cStr)
next
Tip: in the previous example var1, var2, ..., var10 will be defined as attributes.
Tip: The problem with the previous example is that x and cStr will be defined as attributes too!
Note: we can write class definitions inside a string then using eval() we can execute the string to define the classes
42.9 Packages
We can create a package (a group of classes under a common name) using the next syntax
package PackageName
Class Class1
...
Class Class2
...
Class Class3
...
...
Example
o1 = new System.output.console
o1.print("Hello World")
Package System.Output
Class Console
Func Print cText
see cText + nl
Note: we can use the dot operator as part of the package name
Instead of typing the long name PackageName.ClassName we can use the import command
When we import a package, we can use any class inside this package directly.
Example
import system.output
o1 = new console {
print("Hello World")
}
Package System.Output
Class Console
Func Print cText
see cText + nl
42.9. Packages 324
Ring Documentation, Release 1.8
42.10 Printing Objects
We can print the object state (attributes and values) using the see command.
Example:
see new point { x=10 y=20 z=30 }
class point x y z
Output:
x: 10.000000
y: 20.000000
z: 30.000000
42.11 Find() and List of Objects
We can use the find() function to search inside a list of objects.
Syntax:
Find(List,ItemValue,nColumn,cAttribute) ---> Item Index
Example:
myList1 = [new Company {position=3 name="Mahmoud" symbol="MHD"},
new Company {position=2 name="Bert" symbol="BRT"},
new Company {position=1 name="Ring" symbol="RNG"}
]
see find(mylist1,"Bert",1,"name") + nl
see find(mylist1,"Ring",1,"name") + nl
see find(mylist1,"Mahmoud",1,"name") + nl
see find(mylist1,"RNG",1,"symbol") + nl
see find(mylist1,"MHD",1,"symbol") + nl
see find(mylist1,"BRT",1,"symbol") + nl
see find(mylist1,3,1,"position") + nl
see find(mylist1,1,1,"position") + nl
see "Other" + nl
see find(mylist1,"test",1,"name") + nl
see find(mylist1,"test",0,"name") + nl
see find(mylist1,"test",5,"name") + nl
class company position name symbol
Output:
2
3
1
3
1
2
1
3
Other
0
42.10. Printing Objects 325
Ring Documentation, Release 1.8
0
0
42.12 Sort() and List of Objects
We can sort a list of objects based on an object attribute using the Sort() function.
Syntax:
Sort(List,nColumn,cAttribute) ---> Sorted List based on Object Attribute
Example:
myList1 = [
new Company {position=3 name="Mahmoud" symbol="MHD"},
new Company {position=2 name="Bert" symbol="BRT"},
new Company {position=8 name="Charlie" symbol="CHR"},
new Company {position=6 name="Easy" symbol="FEAS"},
new Company {position=7 name="Fox" symbol="EFOX"},
new Company {position=5 name="Dog" symbol="GDOG"},
new Company {position=4 name="George" symbol="DGRG"},
new Company {position=1 name="Ring" symbol="RNG"}
]
see sort(mylist1,1,"name")
see copy("*",70) + nl
see sort(mylist1,1,"symbol")
see copy("*",70) + nl
see sort(mylist1,1,"position")
class company position name symbol
Output:
position: 2.000000
name: Bert
symbol: BRT
position: 8.000000
name: Charlie
symbol: CHR
position: 5.000000
name: Dog
symbol: GDOG
position: 6.000000
name: Easy
symbol: FEAS
position: 7.000000
name: Fox
symbol: EFOX
position: 4.000000
name: George
symbol: DGRG
position: 3.000000
name: Mahmoud
symbol: MHD
position: 1.000000
name: Ring
42.12. Sort() and List of Objects 326
Ring Documentation, Release 1.8
symbol: RNG
**********************************************************************
position: 2.000000
name: Bert
symbol: BRT
position: 8.000000
name: Charlie
symbol: CHR
position: 4.000000
name: George
symbol: DGRG
position: 7.000000
name: Fox
symbol: EFOX
position: 6.000000
name: Easy
symbol: FEAS
position: 5.000000
name: Dog
symbol: GDOG
position: 3.000000
name: Mahmoud
symbol: MHD
position: 1.000000
name: Ring
symbol: RNG
**********************************************************************
position: 1.000000
name: Ring
symbol: RNG
position: 2.000000
name: Bert
symbol: BRT
position: 3.000000
name: Mahmoud
symbol: MHD
position: 4.000000
name: George
symbol: DGRG
position: 5.000000
name: Dog
symbol: GDOG
position: 6.000000
name: Easy
symbol: FEAS
position: 7.000000
name: Fox
symbol: EFOX
position: 8.000000
name: Charlie
symbol: CHR
42.13 Using Self.Attribute and Self.Method()
Inside the class region (After the class name and before any method) and the class methods we can use self.attribute
and self.method()
42.13. Using Self.Attribute and Self.Method() 327
Ring Documentation, Release 1.8
Class Point
self.x = 10
self.y = 20
self.z = 30
func print
see self.x + nl + self.y + nl + self.z + nl
Note: using self.attribute in the class region to define the class attribute protect the class attributes from conflict with
global variables.
Tip: if you typed the class attributes with self.attribute and there are a global variable with the same name it will be
used and the attribute will not be defined.
Check the “Scope Rules” chapter to know about the conflict between the global variable name and the attribute name
Whay this may happens?
Because
• Because in the class region we can access global variables.
• Before defining any variable, Ring try to find the variable and use it if it’s found.
Note: Try to avoid the global variables, use the main function and start their names with $
Tip: In large programs protect your classes and define their members using self.attribute
42.14 Using This.Attribute and This.Method()
Inside class methods we have access to the object scope directly. we don’t need to use Self.attribute or Self.method to
read/write attribute and call methods.
But we can use braces {} while we are inside methods to access another object, In this case the current object scope
will be changed while we are inside the brace.
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
42.14. Using This.Attribute and This.Method() 328
Ring Documentation, Release 1.8
42.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.
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.
42.15. Using This in the class region as Self 329
Ring Documentation, Release 1.8
42.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
42.16. Default value for object attributes 330

More Related Content

PDF
The Ring programming language version 1.5.4 book - Part 31 of 185
PDF
The Ring programming language version 1.9 book - Part 39 of 210
PDF
The Ring programming language version 1.3 book - Part 22 of 88
PDF
The Ring programming language version 1.5.3 book - Part 31 of 184
PDF
The Ring programming language version 1.5.1 book - Part 36 of 180
PDF
The Ring programming language version 1.5.1 book - Part 29 of 180
PDF
The Ring programming language version 1.6 book - Part 40 of 189
PDF
The Ring programming language version 1.5.2 book - Part 30 of 181
The Ring programming language version 1.5.4 book - Part 31 of 185
The Ring programming language version 1.9 book - Part 39 of 210
The Ring programming language version 1.3 book - Part 22 of 88
The Ring programming language version 1.5.3 book - Part 31 of 184
The Ring programming language version 1.5.1 book - Part 36 of 180
The Ring programming language version 1.5.1 book - Part 29 of 180
The Ring programming language version 1.6 book - Part 40 of 189
The Ring programming language version 1.5.2 book - Part 30 of 181

What's hot (20)

PDF
The Ring programming language version 1.8 book - Part 38 of 202
PDF
The Ring programming language version 1.4 book - Part 9 of 30
PDF
The Ring programming language version 1.7 book - Part 34 of 196
PDF
The Ring programming language version 1.6 book - Part 33 of 189
PDF
The Ring programming language version 1.5.4 book - Part 33 of 185
PDF
The Ring programming language version 1.3 book - Part 24 of 88
PDF
The Ring programming language version 1.2 book - Part 22 of 84
PDF
The Ring programming language version 1.5.2 book - Part 32 of 181
PDF
The Ring programming language version 1.6 book - Part 35 of 189
PDF
The Ring programming language version 1.10 book - Part 40 of 212
PPT
Spsl v unit - final
PDF
The Ring programming language version 1.5.4 book - Part 73 of 185
PDF
The Ring programming language version 1.9 book - Part 41 of 210
PDF
The Ring programming language version 1.5.2 book - Part 37 of 181
PDF
The Ring programming language version 1.8 book - Part 80 of 202
PDF
The Ring programming language version 1.5.3 book - Part 83 of 184
PPTX
Lecture 7 arrays
PDF
The Ring programming language version 1.2 book - Part 20 of 84
PDF
The Ring programming language version 1.4.1 book - Part 9 of 31
PDF
The Ring programming language version 1.5.1 book - Part 31 of 180
The Ring programming language version 1.8 book - Part 38 of 202
The Ring programming language version 1.4 book - Part 9 of 30
The Ring programming language version 1.7 book - Part 34 of 196
The Ring programming language version 1.6 book - Part 33 of 189
The Ring programming language version 1.5.4 book - Part 33 of 185
The Ring programming language version 1.3 book - Part 24 of 88
The Ring programming language version 1.2 book - Part 22 of 84
The Ring programming language version 1.5.2 book - Part 32 of 181
The Ring programming language version 1.6 book - Part 35 of 189
The Ring programming language version 1.10 book - Part 40 of 212
Spsl v unit - final
The Ring programming language version 1.5.4 book - Part 73 of 185
The Ring programming language version 1.9 book - Part 41 of 210
The Ring programming language version 1.5.2 book - Part 37 of 181
The Ring programming language version 1.8 book - Part 80 of 202
The Ring programming language version 1.5.3 book - Part 83 of 184
Lecture 7 arrays
The Ring programming language version 1.2 book - Part 20 of 84
The Ring programming language version 1.4.1 book - Part 9 of 31
The Ring programming language version 1.5.1 book - Part 31 of 180
Ad

Similar to The Ring programming language version 1.8 book - Part 36 of 202 (19)

PDF
The Ring programming language version 1.10 book - Part 43 of 212
PDF
The Ring programming language version 1.7 book - Part 37 of 196
PDF
The Ring programming language version 1.5.3 book - Part 33 of 184
PDF
The Ring programming language version 1.9 book - Part 38 of 210
PDF
The Ring programming language version 1.7 book - Part 35 of 196
PDF
The Ring programming language version 1.5.3 book - Part 30 of 184
PDF
The Ring programming language version 1.5.4 book - Part 74 of 185
PDF
The Ring programming language version 1.6 book - Part 32 of 189
PDF
The Ring programming language version 1.5.2 book - Part 71 of 181
PDF
The Ring programming language version 1.2 book - Part 27 of 84
PDF
The Ring programming language version 1.5.4 book - Part 38 of 185
PDF
The Ring programming language version 1.8 book - Part 43 of 202
PDF
The Ring programming language version 1.9 book - Part 98 of 210
PDF
The Ring programming language version 1.10 book - Part 41 of 212
PDF
The Ring programming language version 1.6 book - Part 76 of 189
PDF
The Ring programming language version 1.9 book - Part 46 of 210
PDF
The Ring programming language version 1.10 book - Part 100 of 212
PDF
The Ring programming language version 1.5 book - Part 6 of 31
PDF
The Ring programming language version 1.10 book - Part 86 of 212
The Ring programming language version 1.10 book - Part 43 of 212
The Ring programming language version 1.7 book - Part 37 of 196
The Ring programming language version 1.5.3 book - Part 33 of 184
The Ring programming language version 1.9 book - Part 38 of 210
The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.5.3 book - Part 30 of 184
The Ring programming language version 1.5.4 book - Part 74 of 185
The Ring programming language version 1.6 book - Part 32 of 189
The Ring programming language version 1.5.2 book - Part 71 of 181
The Ring programming language version 1.2 book - Part 27 of 84
The Ring programming language version 1.5.4 book - Part 38 of 185
The Ring programming language version 1.8 book - Part 43 of 202
The Ring programming language version 1.9 book - Part 98 of 210
The Ring programming language version 1.10 book - Part 41 of 212
The Ring programming language version 1.6 book - Part 76 of 189
The Ring programming language version 1.9 book - Part 46 of 210
The Ring programming language version 1.10 book - Part 100 of 212
The Ring programming language version 1.5 book - Part 6 of 31
The Ring programming language version 1.10 book - Part 86 of 212
Ad

More from Mahmoud Samir Fayed (20)

PDF
The Ring programming language version 1.10 book - Part 212 of 212
PDF
The Ring programming language version 1.10 book - Part 211 of 212
PDF
The Ring programming language version 1.10 book - Part 210 of 212
PDF
The Ring programming language version 1.10 book - Part 208 of 212
PDF
The Ring programming language version 1.10 book - Part 207 of 212
PDF
The Ring programming language version 1.10 book - Part 205 of 212
PDF
The Ring programming language version 1.10 book - Part 206 of 212
PDF
The Ring programming language version 1.10 book - Part 204 of 212
PDF
The Ring programming language version 1.10 book - Part 203 of 212
PDF
The Ring programming language version 1.10 book - Part 202 of 212
PDF
The Ring programming language version 1.10 book - Part 201 of 212
PDF
The Ring programming language version 1.10 book - Part 200 of 212
PDF
The Ring programming language version 1.10 book - Part 199 of 212
PDF
The Ring programming language version 1.10 book - Part 198 of 212
PDF
The Ring programming language version 1.10 book - Part 197 of 212
PDF
The Ring programming language version 1.10 book - Part 196 of 212
PDF
The Ring programming language version 1.10 book - Part 195 of 212
PDF
The Ring programming language version 1.10 book - Part 194 of 212
PDF
The Ring programming language version 1.10 book - Part 193 of 212
PDF
The Ring programming language version 1.10 book - Part 192 of 212
The Ring programming language version 1.10 book - Part 212 of 212
The Ring programming language version 1.10 book - Part 211 of 212
The Ring programming language version 1.10 book - Part 210 of 212
The Ring programming language version 1.10 book - Part 208 of 212
The Ring programming language version 1.10 book - Part 207 of 212
The Ring programming language version 1.10 book - Part 205 of 212
The Ring programming language version 1.10 book - Part 206 of 212
The Ring programming language version 1.10 book - Part 204 of 212
The Ring programming language version 1.10 book - Part 203 of 212
The Ring programming language version 1.10 book - Part 202 of 212
The Ring programming language version 1.10 book - Part 201 of 212
The Ring programming language version 1.10 book - Part 200 of 212
The Ring programming language version 1.10 book - Part 199 of 212
The Ring programming language version 1.10 book - Part 198 of 212
The Ring programming language version 1.10 book - Part 197 of 212
The Ring programming language version 1.10 book - Part 196 of 212
The Ring programming language version 1.10 book - Part 195 of 212
The Ring programming language version 1.10 book - Part 194 of 212
The Ring programming language version 1.10 book - Part 193 of 212
The Ring programming language version 1.10 book - Part 192 of 212

Recently uploaded (20)

PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
history of c programming in notes for students .pptx
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Digital Strategies for Manufacturing Companies
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
System and Network Administration Chapter 2
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
CHAPTER 2 - PM Management and IT Context
Which alternative to Crystal Reports is best for small or large businesses.pdf
Reimagine Home Health with the Power of Agentic AI​
history of c programming in notes for students .pptx
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
wealthsignaloriginal-com-DS-text-... (1).pdf
PTS Company Brochure 2025 (1).pdf.......
How Creative Agencies Leverage Project Management Software.pdf
Understanding Forklifts - TECH EHS Solution
Operating system designcfffgfgggggggvggggggggg
Design an Analysis of Algorithms I-SECS-1021-03
Digital Strategies for Manufacturing Companies
VVF-Customer-Presentation2025-Ver1.9.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
2025 Textile ERP Trends: SAP, Odoo & Oracle
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
System and Network Administration Chapter 2

The Ring programming language version 1.8 book - Part 36 of 202

  • 1. Ring Documentation, Release 1.8 Example: o1 = new person { name = "Test" age = 20 print() o1.printsalary() } try see o1.salary catch see cCatchError + nl done try o1.increasesalary(1000) catch see cCatchError + nl done Class Person name age func print see "Name : " + name + nl + "Age : " + age + nl func printsalary see "Salary : " + salary + nl private salary = 15000 func increasesalary x salary += x Output: Name : Test Age : 20 Salary : 15000 Error (R27) : Using private attribute from outside the class : salary Error (R26) : Calling private method from outside the class : increasesalary 42.6 Operator Overloading We can add the operator method to our class to enable using operators with the class objects. Syntax: Class ClassName ... 42.6. Operator Overloading 321
  • 2. Ring Documentation, Release 1.8 Func operator cOperator,Para ... The function operator takes two paramters, the first represent the operator and the second represent the second param- eter after the operator. Example: o1 = new point { x = 10 y = 10 print("P1 : ") } o2 = new point { x = 20 y = 40 print("P2 : ") } o3 = o1 + o2 o3.print("P1+P2 : ") class point x y func operator cOperator,Para result = new point switch cOperator on "+" result.x = x + Para.x result.y = y + Para.y on "-" result.x = x - Para.x result.y = y - Para.y off return result func print cPoint see cPoint + "X : " + x + " Y : " + y + nl Output: P1 : X : 10 Y : 10 P2 : X : 20 Y : 40 P1+P2 : X : 30 Y : 50 The next example from the List class in the stdlib.ring Func operator cOperator,Para result = new list switch cOperator on "+" if isobject(para) for t in Para.vValue vValue + t next but islist(para) for t in Para vValue + t next ok on "len" return len( vValue ) on "[]" return &vValue[para] off return result 42.6. Operator Overloading 322
  • 3. Ring Documentation, Release 1.8 The “len” operator is used with (for in) control structure. The “[]” operator is used when we try to access the list items, In this case we use the & operator to return the item values like strings an numbers by reference, so we can update it when we access the items. 42.7 Inheritance We can create class from another class in the class definition using the keyword from. Syntax: Class <Class Name> [From <Parent Class Name>] We can call a method in the parent class from the child class using the super object. Syntax: func methodname ... super.methodname() ... Example: Func main e1 = new Employee { Name = "test" age = 20 job = "programmer" salary = 20000000 print() } Class Human Name Age func print see "Name : " + name + nl + "Age : " + age + nl Class Employee from Human Job Salary func print super.print() see "Job : " + job + nl + "Salary : " + salary + nl Output: Name : test Age : 20 Job : programmer Salary : 20000000 42.8 Dynamic Attributes We can write instructions after the class name to be executed when we create new objects Example: 42.7. Inheritance 323
  • 4. Ring Documentation, Release 1.8 o1 = new dynamicClass see o1.var5 + nl # output 5 Class DynamicClass for x = 1 to 10 cStr = "var" + x + " = " + x eval(cStr) next Tip: in the previous example var1, var2, ..., var10 will be defined as attributes. Tip: The problem with the previous example is that x and cStr will be defined as attributes too! Note: we can write class definitions inside a string then using eval() we can execute the string to define the classes 42.9 Packages We can create a package (a group of classes under a common name) using the next syntax package PackageName Class Class1 ... Class Class2 ... Class Class3 ... ... Example o1 = new System.output.console o1.print("Hello World") Package System.Output Class Console Func Print cText see cText + nl Note: we can use the dot operator as part of the package name Instead of typing the long name PackageName.ClassName we can use the import command When we import a package, we can use any class inside this package directly. Example import system.output o1 = new console { print("Hello World") } Package System.Output Class Console Func Print cText see cText + nl 42.9. Packages 324
  • 5. Ring Documentation, Release 1.8 42.10 Printing Objects We can print the object state (attributes and values) using the see command. Example: see new point { x=10 y=20 z=30 } class point x y z Output: x: 10.000000 y: 20.000000 z: 30.000000 42.11 Find() and List of Objects We can use the find() function to search inside a list of objects. Syntax: Find(List,ItemValue,nColumn,cAttribute) ---> Item Index Example: myList1 = [new Company {position=3 name="Mahmoud" symbol="MHD"}, new Company {position=2 name="Bert" symbol="BRT"}, new Company {position=1 name="Ring" symbol="RNG"} ] see find(mylist1,"Bert",1,"name") + nl see find(mylist1,"Ring",1,"name") + nl see find(mylist1,"Mahmoud",1,"name") + nl see find(mylist1,"RNG",1,"symbol") + nl see find(mylist1,"MHD",1,"symbol") + nl see find(mylist1,"BRT",1,"symbol") + nl see find(mylist1,3,1,"position") + nl see find(mylist1,1,1,"position") + nl see "Other" + nl see find(mylist1,"test",1,"name") + nl see find(mylist1,"test",0,"name") + nl see find(mylist1,"test",5,"name") + nl class company position name symbol Output: 2 3 1 3 1 2 1 3 Other 0 42.10. Printing Objects 325
  • 6. Ring Documentation, Release 1.8 0 0 42.12 Sort() and List of Objects We can sort a list of objects based on an object attribute using the Sort() function. Syntax: Sort(List,nColumn,cAttribute) ---> Sorted List based on Object Attribute Example: myList1 = [ new Company {position=3 name="Mahmoud" symbol="MHD"}, new Company {position=2 name="Bert" symbol="BRT"}, new Company {position=8 name="Charlie" symbol="CHR"}, new Company {position=6 name="Easy" symbol="FEAS"}, new Company {position=7 name="Fox" symbol="EFOX"}, new Company {position=5 name="Dog" symbol="GDOG"}, new Company {position=4 name="George" symbol="DGRG"}, new Company {position=1 name="Ring" symbol="RNG"} ] see sort(mylist1,1,"name") see copy("*",70) + nl see sort(mylist1,1,"symbol") see copy("*",70) + nl see sort(mylist1,1,"position") class company position name symbol Output: position: 2.000000 name: Bert symbol: BRT position: 8.000000 name: Charlie symbol: CHR position: 5.000000 name: Dog symbol: GDOG position: 6.000000 name: Easy symbol: FEAS position: 7.000000 name: Fox symbol: EFOX position: 4.000000 name: George symbol: DGRG position: 3.000000 name: Mahmoud symbol: MHD position: 1.000000 name: Ring 42.12. Sort() and List of Objects 326
  • 7. Ring Documentation, Release 1.8 symbol: RNG ********************************************************************** position: 2.000000 name: Bert symbol: BRT position: 8.000000 name: Charlie symbol: CHR position: 4.000000 name: George symbol: DGRG position: 7.000000 name: Fox symbol: EFOX position: 6.000000 name: Easy symbol: FEAS position: 5.000000 name: Dog symbol: GDOG position: 3.000000 name: Mahmoud symbol: MHD position: 1.000000 name: Ring symbol: RNG ********************************************************************** position: 1.000000 name: Ring symbol: RNG position: 2.000000 name: Bert symbol: BRT position: 3.000000 name: Mahmoud symbol: MHD position: 4.000000 name: George symbol: DGRG position: 5.000000 name: Dog symbol: GDOG position: 6.000000 name: Easy symbol: FEAS position: 7.000000 name: Fox symbol: EFOX position: 8.000000 name: Charlie symbol: CHR 42.13 Using Self.Attribute and Self.Method() Inside the class region (After the class name and before any method) and the class methods we can use self.attribute and self.method() 42.13. Using Self.Attribute and Self.Method() 327
  • 8. Ring Documentation, Release 1.8 Class Point self.x = 10 self.y = 20 self.z = 30 func print see self.x + nl + self.y + nl + self.z + nl Note: using self.attribute in the class region to define the class attribute protect the class attributes from conflict with global variables. Tip: if you typed the class attributes with self.attribute and there are a global variable with the same name it will be used and the attribute will not be defined. Check the “Scope Rules” chapter to know about the conflict between the global variable name and the attribute name Whay this may happens? Because • Because in the class region we can access global variables. • Before defining any variable, Ring try to find the variable and use it if it’s found. Note: Try to avoid the global variables, use the main function and start their names with $ Tip: In large programs protect your classes and define their members using self.attribute 42.14 Using This.Attribute and This.Method() Inside class methods we have access to the object scope directly. we don’t need to use Self.attribute or Self.method to read/write attribute and call methods. But we can use braces {} while we are inside methods to access another object, In this case the current object scope will be changed while we are inside the brace. 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 42.14. Using This.Attribute and This.Method() 328
  • 9. Ring Documentation, Release 1.8 42.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. 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. 42.15. Using This in the class region as Self 329
  • 10. Ring Documentation, Release 1.8 42.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 42.16. Default value for object attributes 330