Академический Документы
Профессиональный Документы
Культура Документы
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 665
Ring Documentation, Release 1.5
========================================
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 666
Ring Documentation, Release 1.5
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 667
CHAPTER
SIXTYTWO
SYNTAX FLEXIBILITY
In this chapter we will learn about some options that are provided automatically by the Ring compiler for syntax
flexibility.
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
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
668
Ring Documentation, Release 1.5
x--
endwhile
test()
function test
print "message from test" + nl
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 _+ +
Print 5 plus 5
changeringoperator plus +
changeringkeyword PRINT SEE
You may store a group of ChangeRingKeyword and ChangeRingOperator commands in a file to use later in many
source files. You cant 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 doesnt 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
Tip: The effect of LoadSyntax command is related to the current source code file only.
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)
Output:
7
7
Output:
Hello
How are you?
Welcome to Ring
one
two
three
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
class test
@instance_variable = 10
func hello
local_variable = 15
Output:
Global : 5
Instance : 10
Local : 15
if you dont 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
if you dont like the other keyword in switch statement Then you can use the else keyword.
Also you can replace else with other in if statement.
i.e. other keyword is the same as else keyword.
Example:
x = 1
switch x
on 10
see "10" + nl
else
see "not 10" + nl
end
Output:
not 10
switch x
on 1 see "one" + nl
on 2 see "two" + nl
end
Output:
if statement..
one
for loop..
12345678910
switch...
one
try catch...
catching error
switch x {
on 1 see "one" + nl
on 2 see "two" + nl
}
62.10. Using braces to start and end different control structures 674