SlideShare a Scribd company logo
Ring Documentation, Release 1.5.4
5 - Before defining any variable (in any scope and in the class region too) a search process will be done to use the
variable if it’s found.
6 - Functions and Methods parameters are defined automatically as local variables to these functions or methods.
7 - Using Object.Attribute will search in the object attributes only.
8 - Using Self.Attribute will lead to a search for Self first then search in Self Attributes.
9 - The Self reference inside class region (after the class name and before any method) always point to the object scope
created from the class.
10- The Self reference inside methods will be changed when we uses Braces to be a reference to the object that we
access.
11- Writing variable names directly in the class region (after the class name and before any method) means using them
or define then (in order).
12- Using self.attribute in the class region reduce search to the object scope (avoid conflict with global scope).
From these rules you can understand all types of conflicts and why you may have them and how to avoid them
Simple advices to avoid any conflict and use the scope rules in a better way
1 - Try to avoid global variables
2 - Use the Main Function - This will help you to avoid global variables
3 - If you are going to use many global variables use the $ mark before the variable name
4 - In the class region if you don’t respect the advice number three ($) then use self.attribute when you define your
attributes
5 - You can use object.attribute and object.method() instead of object { attribute } and object { method() } if you don’t
like changing the object scope.
6 - If you will use nested braces in a class - think about using the class region if possible because in this region you
will have access to the object that you access using { } + access to the class attributes
7 - If you are inside a class method and used nested braces you will change the object scope with each brace and you
will loss the access to the class attributes directly but you have access to the local scope before and after using brace
{ } , if you will read/modify the class attribute from braces then use This.Attribute because using ‘This’ means (The
object created from this class) while using ‘Self’ means (The object in the current object scope).
After understanding all of the previous points, You will master this topic.
60.14. Summary of Scope Rules 705
CHAPTER
SIXTYONE
SCOPE RULES FOR FUNCTIONS AND METHODS
In this chapter we will learn about the scope rules for functions and methods.
You need to know the next information once you started using Ring for large applications.
These applications may contains and use
• Many Packages and Classes written in Ring
• Many Functions written in Ring
• Standard Ring Functions (Written in C language)
• Functions and Classes written in C/C++ languages
61.1 How Ring find the Functions and Methods?
When you call a method or function, Ring will start a search process to find this function
If found –> Call the function and store the function pointer in the cache so Ring can use it again with doing another
search.
If not found —> Runtime error message (That you can avoid using Try/Catch)
How the search process is done?
Search for functions/methods follow the next order
1 - Search in methods (if we are inside class method or object using braces {})
2 - Search in functions written by the programmer using Ring Code
3 - Search in functions written in C/C++ like standard Ring functions
This enable us to write clean code inside classes methods and avoid any conflict with functions.
If we want to call a function with the same name as a method in the class we will need a wrapper function or we will
access a temp. object using { } then call that function there.
We can replace C/C++ Functions with Ring Functions.
We can replace Ring Functions with Ring Methods.
Note: Using self.method() is not necessary in any use case.
Tip: We can use this.method() to escape from the current active scope that we access using braces {} and call a
method in the class that we are inside.
706
Ring Documentation, Release 1.5.4
61.2 Example about Sharing Names between Functions and Methods
Look at the next example
func main
o1 = new myclass { test() test2() }
test2()
func f1
see "f1 function" + nl
func f2
see "f2 function" + nl
func f3
see "f3 function" + nl
func test2
myline()
see "test2 function" + nl
new myclass {
f1()
f2()
f3()
self.f3()
}
myobj = new myclass
myobj.f3()
myline()
func myline
see copy("=",40) + nl
Class myclass
func test
myline()
see "test method" + nl
f1()
f2()
f3()
myline()
func f3
see "f3 method" + nl
func test2
myline()
see "test2 method" + nl
self {
f1()
f2()
f3()
}
myline()
Output:
61.2. Example about Sharing Names between Functions and Methods 707
Ring Documentation, Release 1.5.4
========================================
test method
f1 function
f2 function
f3 method
========================================
========================================
test2 method
f1 function
f2 function
f3 method
========================================
========================================
test2 function
f1 function
f2 function
f3 method
f3 method
f3 method
========================================
61.3 Calling a function sharing the name with a method in the current
class
In the previous example we have a function called f3() and we have a method called f3()
How we can call the f3() function from the test() method ?
Solution (1) : Change the current object scope to another object scope
In this solution we will have an empty class called local that we will use to change the current object scope.
func main
o1 = new myclass { test()}
func f1
see "f1 function" + nl
func f2
see "f2 function" + nl
func f3
see "f3 function" + nl
func myline
see copy("=",40) + nl
Class myclass
func test
myline()
see "test method" + nl
f1()
f2()
f3() # call f3() method
new local { f3() } # call f3() function
myline()
61.3. Calling a function sharing the name with a method in the current class 708
Ring Documentation, Release 1.5.4
func f3
see "f3 method" + nl
class local
Output:
========================================
test method
f1 function
f2 function
f3 method
f3 function
========================================
61.3. Calling a function sharing the name with a method in the current class 709
CHAPTER
SIXTYTWO
SYNTAX FLEXIBILITY
In this chapter we will learn about some options that are provided automatically by the Ring compiler for syntax
flexibility.
62.1 Change Language Keywords
We can change any keyword using the ChangeRingKeyword command.
Note: Remember to restore the keyword again if the team will mix between styles in the same project.
Tip: The ChangeRingKeyword command is executed in the scanner stage by the compiler (before parsing).
Syntax:
ChangeRingKeyword <oldkeyword> <newkeyword>
Example:
ChangeRingKeyword see print
print "welcome" + nl
ChangeRingKeyword print see
see "Welcome" + nl
Example:
ChangeRingKeyword func function
ChangeRingKeyword see print
ChangeRingKeyword ok endif
ChangeRingKeyword next endfor
ChangeRingKeyword end endwhile
x = 10
while x > 0
print "x = " + x + nl
for t = 1 to 10
if t = 3
print "number three" + nl
endif
endfor
710
Ring Documentation, Release 1.5.4
x--
endwhile
test()
function test
print "message from test" + nl
ChangeRingKeyword function func
ChangeRingKeyword print see
ChangeRingKeyword endif ok
ChangeRingKeyword endfor next
ChangeRingKeyword endwhile end
62.2 Change Language Operators
We can change any operator using the ChangeRingOperator command.
Note: Remember to restore the operator again if the team will mix between styles in the same project.
Tip: The ChangeRingOperartor command is executed in the scanner stage by the compiler (before parsing).
Syntax:
ChangeRingOperator <oldkeyword> <newkeyword>
Example:
The next program hide the + operator by changing it to _+
changeringoperator + _+
changeringkeyword SEE PRINT
try
print 5 + 10
catch
print nl print "error" print nl
done
changeringoperator _+ +
The next program change the + operator to “plus”.
changeringoperator + plus
changeringkeyword SEE PRINT
Print 5 plus 5
changeringoperator plus +
changeringkeyword PRINT SEE
62.2. Change Language Operators 711
Ring Documentation, Release 1.5.4
62.3 Load Syntax Files
You may store a group of ChangeRingKeyword and ChangeRingOperator commands in a file to use later in many
source files. You can’t use the Load command to call these files because
• ChangeRingKeyword and ChangeRingOperator commands are executed in the scanner phase by the compiler
(before parsing).
• The load command is executed in the parsing phase (after the scanner phase).
Solution: Use the LoadSyntax Command which is executed in the scanner phase.
Syntax:
LoadSyntax "syntaxfile.ring"
Example:
File : StyleBasicOn.ring
ChangeRingKeyword see print
ChangeRingKeyword ok endif
ChangeRingKeyword next endfor
ChangeRingKeyword end endwhile
File : StyleBasicOff.ring
ChangeRingKeyword print see
ChangeRingKeyword endif ok
ChangeRingKeyword endfor next
ChangeRingKeyword endwhile end
File : UseStyleBasic.ring
LoadSyntax "stylebasicon.ring"
x = 10
while x > 0
print "x = " + x + nl
for t = 1 to 10
if t = 3
print "number three" + nl
endif
endfor
x--
endwhile
LoadSyntax "stylebasicoff.ring"
see "done" + nl
Note: files called by the LoadSyntax command must contains ChangeRingKeyword and ChangeRingOperator com-
mands only.
Tip: files called by the LoadSyntax command doesn’t support functions, packages and classes. just imperative
commands only.
Note: Using this feature you can create many styles that you can use in the same project and you can support Ring
62.3. Load Syntax Files 712
Ring Documentation, Release 1.5.4
translation to other languages like Arabic, French and so on.
Tip: The effect of LoadSyntax command is related to the current source code file only.
62.4 Using “()” around the function parameters
We can use () around the function parameters (optional).
Example:
hello()
sum(3,4)
func hello()
see "Hello" + nl
func sum(x,y)
see x+y+nl
Output:
Hello
7
Example:
myfunc = func x,y { see x + y + nl }
call myfunc (3,4)
myfunc2 = func (x,y) { see x+y+nl }
call myfunc(3,4)
Output:
7
7
62.5 Using Semi-colon after and between statements
In Ring we can use semi-colon after and between statements (optional).
Example:
# Using semi-colon is optional
see "Hello" + nl ; see "How are you?" + nl ; see "Welcome to Ring" + nl ;
one() ; two() ; three() ;
func one ; see "one" + nl ;
func two ; see "two" + nl ;
func three ; see "three" + nl ;
Output:
62.4. Using “()” around the function parameters 713
Ring Documentation, Release 1.5.4
Hello
How are you?
Welcome to Ring
one
two
three
62.6 Using $ and @ in the start of the variable name
You can use any unicode character in the variable name also we can use $ and @ in the name.
This feature may help, for example we can start global variables with $ and the object attributes with @.
In other languages like Ruby this is the rule, In the Ring language this is just an option without any force from the
Compiler.
example:
$global_variable = 5
new test { hello() }
class test
@instance_variable = 10
func hello
local_variable = 15
see "Global : " + $global_variable + nl +
"Instance : " + @instance_variable + nl +
"Local : " + local_variable + nl
Output:
Global : 5
Instance : 10
Local : 15
62.7 Using the ‘elseif’ keyword as ‘but’ in if statement
if you don’t like the ‘but’ keyword in if statement Then you can use the ‘elseif’ keyword.
Example:
give x
if x = 1 see "one"
elseif x=2 see "two"
elseif x=3 see "three"
elseif x=4 see "four"
else see "other"
ok
see nl
62.6. Using $ and @ in the start of the variable name 714

More Related Content

PDF
The Ring programming language version 1.8 book - Part 81 of 202
PDF
The Ring programming language version 1.5.1 book - Part 70 of 180
PDF
The Ring programming language version 1.5.2 book - Part 71 of 181
PDF
The Ring programming language version 1.5.4 book - Part 72 of 185
PDF
The Ring programming language version 1.8 book - Part 79 of 202
PDF
The Ring programming language version 1.10 book - Part 85 of 212
PDF
The Ring programming language version 1.9 book - Part 83 of 210
PDF
The Ring programming language version 1.10 book - Part 86 of 212
The Ring programming language version 1.8 book - Part 81 of 202
The Ring programming language version 1.5.1 book - Part 70 of 180
The Ring programming language version 1.5.2 book - Part 71 of 181
The Ring programming language version 1.5.4 book - Part 72 of 185
The Ring programming language version 1.8 book - Part 79 of 202
The Ring programming language version 1.10 book - Part 85 of 212
The Ring programming language version 1.9 book - Part 83 of 210
The Ring programming language version 1.10 book - Part 86 of 212

What's hot (19)

PDF
The Ring programming language version 1.5.1 book - Part 69 of 180
PDF
The Ring programming language version 1.5.2 book - Part 69 of 181
PPT
Effective Java - Generics
ODP
Scala oo (1)
PPTX
02 Java Language And OOP Part II LAB
ODP
Functional Objects & Function and Closures
PPTX
Lecture - 5 Control Statement
PDF
Unit Testing in Angular(7/8/9) Using Jasmine and Karma Part-2
PPT
Java Generics
PPTX
Design patterns(red)
PDF
Java Generics - by Example
PPT
Core Java Concepts
PPT
Chapter 9 - Characters and Strings
PPTX
Lecture - 3 Variables-data type_operators_oops concept
PPTX
01 Java Language And OOP Part I LAB
PDF
Java 8 ​and ​Best Practices
PPTX
Lecture 6 inheritance
PPTX
Java generics final
DOCX
Autoboxing and unboxing
The Ring programming language version 1.5.1 book - Part 69 of 180
The Ring programming language version 1.5.2 book - Part 69 of 181
Effective Java - Generics
Scala oo (1)
02 Java Language And OOP Part II LAB
Functional Objects & Function and Closures
Lecture - 5 Control Statement
Unit Testing in Angular(7/8/9) Using Jasmine and Karma Part-2
Java Generics
Design patterns(red)
Java Generics - by Example
Core Java Concepts
Chapter 9 - Characters and Strings
Lecture - 3 Variables-data type_operators_oops concept
01 Java Language And OOP Part I LAB
Java 8 ​and ​Best Practices
Lecture 6 inheritance
Java generics final
Autoboxing and unboxing
Ad

Similar to The Ring programming language version 1.5.4 book - Part 74 of 185 (20)

PDF
The Ring programming language version 1.9 book - Part 85 of 210
PDF
The Ring programming language version 1.6 book - Part 76 of 189
PDF
The Ring programming language version 1.5.3 book - Part 84 of 184
PDF
The Ring programming language version 1.5.2 book - Part 30 of 181
PDF
The Ring programming language version 1.5.3 book - Part 31 of 184
PDF
The Ring programming language version 1.7 book - Part 79 of 196
PDF
The Ring programming language version 1.5.4 book - Part 31 of 185
PDF
The Ring programming language version 1.7 book - Part 34 of 196
PDF
The Ring programming language version 1.7 book - Part 35 of 196
PDF
The Ring programming language version 1.3 book - Part 22 of 88
PDF
The Ring programming language version 1.5.1 book - Part 29 of 180
PDF
The Ring programming language version 1.5.1 book - Part 71 of 180
PDF
The Ring programming language version 1.10 book - Part 87 of 212
PDF
The Ring programming language version 1.6 book - Part 33 of 189
PDF
The Ring programming language version 1.9 book - Part 39 of 210
PDF
The Ring programming language version 1.4.1 book - Part 29 of 31
PDF
The Ring programming language version 1.2 book - Part 20 of 84
PDF
The Ring programming language version 1.10 book - Part 43 of 212
PDF
The Ring programming language version 1.6 book - Part 77 of 189
PDF
The Ring programming language version 1.5.3 book - Part 82 of 184
The Ring programming language version 1.9 book - Part 85 of 210
The Ring programming language version 1.6 book - Part 76 of 189
The Ring programming language version 1.5.3 book - Part 84 of 184
The Ring programming language version 1.5.2 book - Part 30 of 181
The Ring programming language version 1.5.3 book - Part 31 of 184
The Ring programming language version 1.7 book - Part 79 of 196
The Ring programming language version 1.5.4 book - Part 31 of 185
The Ring programming language version 1.7 book - Part 34 of 196
The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.3 book - Part 22 of 88
The Ring programming language version 1.5.1 book - Part 29 of 180
The Ring programming language version 1.5.1 book - Part 71 of 180
The Ring programming language version 1.10 book - Part 87 of 212
The Ring programming language version 1.6 book - Part 33 of 189
The Ring programming language version 1.9 book - Part 39 of 210
The Ring programming language version 1.4.1 book - Part 29 of 31
The Ring programming language version 1.2 book - Part 20 of 84
The Ring programming language version 1.10 book - Part 43 of 212
The Ring programming language version 1.6 book - Part 77 of 189
The Ring programming language version 1.5.3 book - Part 82 of 184
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
Programs and apps: productivity, graphics, security and other tools
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Big Data Technologies - Introduction.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Programs and apps: productivity, graphics, security and other tools
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Chapter 3 Spatial Domain Image Processing.pdf
Electronic commerce courselecture one. Pdf
NewMind AI Weekly Chronicles - August'25 Week I
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Understanding_Digital_Forensics_Presentation.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Spectral efficient network and resource selection model in 5G networks
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Encapsulation_ Review paper, used for researhc scholars
The Rise and Fall of 3GPP – Time for a Sabbatical?
Unlocking AI with Model Context Protocol (MCP)
Big Data Technologies - Introduction.pptx
Network Security Unit 5.pdf for BCA BBA.
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
sap open course for s4hana steps from ECC to s4
Profit Center Accounting in SAP S/4HANA, S4F28 Col11

The Ring programming language version 1.5.4 book - Part 74 of 185

  • 1. Ring Documentation, Release 1.5.4 5 - Before defining any variable (in any scope and in the class region too) a search process will be done to use the variable if it’s found. 6 - Functions and Methods parameters are defined automatically as local variables to these functions or methods. 7 - Using Object.Attribute will search in the object attributes only. 8 - Using Self.Attribute will lead to a search for Self first then search in Self Attributes. 9 - The Self reference inside class region (after the class name and before any method) always point to the object scope created from the class. 10- The Self reference inside methods will be changed when we uses Braces to be a reference to the object that we access. 11- Writing variable names directly in the class region (after the class name and before any method) means using them or define then (in order). 12- Using self.attribute in the class region reduce search to the object scope (avoid conflict with global scope). From these rules you can understand all types of conflicts and why you may have them and how to avoid them Simple advices to avoid any conflict and use the scope rules in a better way 1 - Try to avoid global variables 2 - Use the Main Function - This will help you to avoid global variables 3 - If you are going to use many global variables use the $ mark before the variable name 4 - In the class region if you don’t respect the advice number three ($) then use self.attribute when you define your attributes 5 - You can use object.attribute and object.method() instead of object { attribute } and object { method() } if you don’t like changing the object scope. 6 - If you will use nested braces in a class - think about using the class region if possible because in this region you will have access to the object that you access using { } + access to the class attributes 7 - If you are inside a class method and used nested braces you will change the object scope with each brace and you will loss the access to the class attributes directly but you have access to the local scope before and after using brace { } , if you will read/modify the class attribute from braces then use This.Attribute because using ‘This’ means (The object created from this class) while using ‘Self’ means (The object in the current object scope). After understanding all of the previous points, You will master this topic. 60.14. Summary of Scope Rules 705
  • 2. CHAPTER SIXTYONE SCOPE RULES FOR FUNCTIONS AND METHODS In this chapter we will learn about the scope rules for functions and methods. You need to know the next information once you started using Ring for large applications. These applications may contains and use • Many Packages and Classes written in Ring • Many Functions written in Ring • Standard Ring Functions (Written in C language) • Functions and Classes written in C/C++ languages 61.1 How Ring find the Functions and Methods? When you call a method or function, Ring will start a search process to find this function If found –> Call the function and store the function pointer in the cache so Ring can use it again with doing another search. If not found —> Runtime error message (That you can avoid using Try/Catch) How the search process is done? Search for functions/methods follow the next order 1 - Search in methods (if we are inside class method or object using braces {}) 2 - Search in functions written by the programmer using Ring Code 3 - Search in functions written in C/C++ like standard Ring functions This enable us to write clean code inside classes methods and avoid any conflict with functions. If we want to call a function with the same name as a method in the class we will need a wrapper function or we will access a temp. object using { } then call that function there. We can replace C/C++ Functions with Ring Functions. We can replace Ring Functions with Ring Methods. Note: Using self.method() is not necessary in any use case. Tip: We can use this.method() to escape from the current active scope that we access using braces {} and call a method in the class that we are inside. 706
  • 3. Ring Documentation, Release 1.5.4 61.2 Example about Sharing Names between Functions and Methods Look at the next example func main o1 = new myclass { test() test2() } test2() func f1 see "f1 function" + nl func f2 see "f2 function" + nl func f3 see "f3 function" + nl func test2 myline() see "test2 function" + nl new myclass { f1() f2() f3() self.f3() } myobj = new myclass myobj.f3() myline() func myline see copy("=",40) + nl Class myclass func test myline() see "test method" + nl f1() f2() f3() myline() func f3 see "f3 method" + nl func test2 myline() see "test2 method" + nl self { f1() f2() f3() } myline() Output: 61.2. Example about Sharing Names between Functions and Methods 707
  • 4. Ring Documentation, Release 1.5.4 ======================================== test method f1 function f2 function f3 method ======================================== ======================================== test2 method f1 function f2 function f3 method ======================================== ======================================== test2 function f1 function f2 function f3 method f3 method f3 method ======================================== 61.3 Calling a function sharing the name with a method in the current class In the previous example we have a function called f3() and we have a method called f3() How we can call the f3() function from the test() method ? Solution (1) : Change the current object scope to another object scope In this solution we will have an empty class called local that we will use to change the current object scope. func main o1 = new myclass { test()} func f1 see "f1 function" + nl func f2 see "f2 function" + nl func f3 see "f3 function" + nl func myline see copy("=",40) + nl Class myclass func test myline() see "test method" + nl f1() f2() f3() # call f3() method new local { f3() } # call f3() function myline() 61.3. Calling a function sharing the name with a method in the current class 708
  • 5. Ring Documentation, Release 1.5.4 func f3 see "f3 method" + nl class local Output: ======================================== test method f1 function f2 function f3 method f3 function ======================================== 61.3. Calling a function sharing the name with a method in the current class 709
  • 6. CHAPTER SIXTYTWO SYNTAX FLEXIBILITY In this chapter we will learn about some options that are provided automatically by the Ring compiler for syntax flexibility. 62.1 Change Language Keywords We can change any keyword using the ChangeRingKeyword command. Note: Remember to restore the keyword again if the team will mix between styles in the same project. Tip: The ChangeRingKeyword command is executed in the scanner stage by the compiler (before parsing). Syntax: ChangeRingKeyword <oldkeyword> <newkeyword> Example: ChangeRingKeyword see print print "welcome" + nl ChangeRingKeyword print see see "Welcome" + nl Example: ChangeRingKeyword func function ChangeRingKeyword see print ChangeRingKeyword ok endif ChangeRingKeyword next endfor ChangeRingKeyword end endwhile x = 10 while x > 0 print "x = " + x + nl for t = 1 to 10 if t = 3 print "number three" + nl endif endfor 710
  • 7. Ring Documentation, Release 1.5.4 x-- endwhile test() function test print "message from test" + nl ChangeRingKeyword function func ChangeRingKeyword print see ChangeRingKeyword endif ok ChangeRingKeyword endfor next ChangeRingKeyword endwhile end 62.2 Change Language Operators We can change any operator using the ChangeRingOperator command. Note: Remember to restore the operator again if the team will mix between styles in the same project. Tip: The ChangeRingOperartor command is executed in the scanner stage by the compiler (before parsing). Syntax: ChangeRingOperator <oldkeyword> <newkeyword> Example: The next program hide the + operator by changing it to _+ changeringoperator + _+ changeringkeyword SEE PRINT try print 5 + 10 catch print nl print "error" print nl done changeringoperator _+ + The next program change the + operator to “plus”. changeringoperator + plus changeringkeyword SEE PRINT Print 5 plus 5 changeringoperator plus + changeringkeyword PRINT SEE 62.2. Change Language Operators 711
  • 8. Ring Documentation, Release 1.5.4 62.3 Load Syntax Files You may store a group of ChangeRingKeyword and ChangeRingOperator commands in a file to use later in many source files. You can’t use the Load command to call these files because • ChangeRingKeyword and ChangeRingOperator commands are executed in the scanner phase by the compiler (before parsing). • The load command is executed in the parsing phase (after the scanner phase). Solution: Use the LoadSyntax Command which is executed in the scanner phase. Syntax: LoadSyntax "syntaxfile.ring" Example: File : StyleBasicOn.ring ChangeRingKeyword see print ChangeRingKeyword ok endif ChangeRingKeyword next endfor ChangeRingKeyword end endwhile File : StyleBasicOff.ring ChangeRingKeyword print see ChangeRingKeyword endif ok ChangeRingKeyword endfor next ChangeRingKeyword endwhile end File : UseStyleBasic.ring LoadSyntax "stylebasicon.ring" x = 10 while x > 0 print "x = " + x + nl for t = 1 to 10 if t = 3 print "number three" + nl endif endfor x-- endwhile LoadSyntax "stylebasicoff.ring" see "done" + nl Note: files called by the LoadSyntax command must contains ChangeRingKeyword and ChangeRingOperator com- mands only. Tip: files called by the LoadSyntax command doesn’t support functions, packages and classes. just imperative commands only. Note: Using this feature you can create many styles that you can use in the same project and you can support Ring 62.3. Load Syntax Files 712
  • 9. Ring Documentation, Release 1.5.4 translation to other languages like Arabic, French and so on. Tip: The effect of LoadSyntax command is related to the current source code file only. 62.4 Using “()” around the function parameters We can use () around the function parameters (optional). Example: hello() sum(3,4) func hello() see "Hello" + nl func sum(x,y) see x+y+nl Output: Hello 7 Example: myfunc = func x,y { see x + y + nl } call myfunc (3,4) myfunc2 = func (x,y) { see x+y+nl } call myfunc(3,4) Output: 7 7 62.5 Using Semi-colon after and between statements In Ring we can use semi-colon after and between statements (optional). Example: # Using semi-colon is optional see "Hello" + nl ; see "How are you?" + nl ; see "Welcome to Ring" + nl ; one() ; two() ; three() ; func one ; see "one" + nl ; func two ; see "two" + nl ; func three ; see "three" + nl ; Output: 62.4. Using “()” around the function parameters 713
  • 10. Ring Documentation, Release 1.5.4 Hello How are you? Welcome to Ring one two three 62.6 Using $ and @ in the start of the variable name You can use any unicode character in the variable name also we can use $ and @ in the name. This feature may help, for example we can start global variables with $ and the object attributes with @. In other languages like Ruby this is the rule, In the Ring language this is just an option without any force from the Compiler. example: $global_variable = 5 new test { hello() } class test @instance_variable = 10 func hello local_variable = 15 see "Global : " + $global_variable + nl + "Instance : " + @instance_variable + nl + "Local : " + local_variable + nl Output: Global : 5 Instance : 10 Local : 15 62.7 Using the ‘elseif’ keyword as ‘but’ in if statement if you don’t like the ‘but’ keyword in if statement Then you can use the ‘elseif’ keyword. Example: give x if x = 1 see "one" elseif x=2 see "two" elseif x=3 see "three" elseif x=4 see "four" else see "other" ok see nl 62.6. Using $ and @ in the start of the variable name 714