SlideShare a Scribd company logo
Ring Documentation, Release 1.7
34.7 IsMacOSX() Function
We can check if the operating system is macOS or not using the IsMacOSX() function
Syntax:
IsMacOSX() ---> Returns 1 if the operating system is Mac OS X, Returns 0 if it's not
34.8 IsLinux() Function
We can check if the operating system is Linux or not using the IsLinux() function
Syntax:
IsLinux() ---> Returns 1 if the operating system is Linux, Returns 0 if it's not
34.9 IsFreeBSD() Function
We can check if the operating system is FreeBSD or not using the IsFreeBSD() function
Syntax:
IsFreeBSD() ---> Returns 1 if the operating system is FreeBSD, Returns 0 if it's not
34.10 IsAndroid() Function
We can check if the operating system is Android or not using the IsAndroid() function
Syntax:
IsAndroid() ---> Returns 1 if the operating system is Android, Returns 0 if it's not
34.11 Example
see "IsMSDOS() --> " + ismsdos() + nl
see "IsWindows() --> " + iswindows() + nl
see "IsWindows64() --> " + iswindows64() + nl
see "IsUnix() --> " + isunix() + nl
see "IsMacOSX() --> " + ismacosx() + nl
see "IsLinux() --> " + islinux() + nl
see "IsFreeBSD() --> " + isfreebsd() + nl
see "IsAndroid() --> " + isandroid() + nl
Output:
IsMSDOS() --> 0
IsWindows() --> 1
IsWindows64() --> 0
IsUnix() --> 0
IsMacOSX() --> 0
34.7. IsMacOSX() Function 262
Ring Documentation, Release 1.7
IsLinux() --> 0
IsFreeBSD() --> 0
IsAndroid() --> 0
34.12 Windowsnl() Function
We can get the windows new line string using the Windowsnl() function.
Syntax:
WindowsNL() ---> Returns a string contains CR+LF = CHAR(13) + CHAR(10)
Example:
cStr = read("input.txt")
if iswindows()
cStr = substr(cStr,windowsnl(),nl)
ok
aList = str2list(cStr)
# to do - list items processing using "for in"
cStr = list2str(aList)
if iswindows()
cStr = substr(cStr,nl,windowsnl())
ok
write("ouput.txt",cStr)
34.13 Get Command Line Arguments
We can get the command line arguments passed to the ring script using the sysargv variable.
The sysargv variable is a list contains the command line parameters.
Example
see copy("=",30) + nl
see "Command Line Parameters" + nl
see "Size : " + len(sysargv) + nl
see sysargv
see copy("=",30) + nl
if len(sysargv) < 4 return ok
nStart = sysargv[3]
nEnd = sysargv[4]
for x = nStart to nEnd
see x + nl
next
Output
b:mahmoudappsring>ring testssyspara.ring 1 10
==============================
Command Line Parameters
34.12. Windowsnl() Function 263
Ring Documentation, Release 1.7
Size : 4
ring
testssyspara.ring
1
10
==============================
1
2
3
4
5
6
7
8
9
10
34.14 Get Active Source File Name
We can get the active source file name (*.ring) using the filename() function
Syntax:
filename() ---> String contains the active source file name.
Example:
see "Active Source File Name : " + filename() + nl
Output:
Active Source File Name : testsfilename.ring
Example:
if sysargv[2] = filename()
see "I'm the main program file!" + nl
# we can run tests here!
else
see "I'm a sub file in a program" + nl
ok
34.15 PrevFileName() Function
Using the PrevFileName() function we can get the previous active source file name.
The previous file would be the file of the caller function, Or the file of the function that we called before calling
PrevFileName().
Syntax:
prevfilename() ---> String contains the previous source file name.
Example:
The next function in stdlib.ring uses the PrevFileName() to know if the file of the caller function is the main source
file of the program or not.
34.14. Get Active Source File Name 264
Ring Documentation, Release 1.7
Func IsMainSourceFile
if PrevFileName() = sysargv[2]
return true
ok
return false
34.16 CurrentDir() Function
Return the path of the current directory
Syntax:
CurrenDir() ---> String contains the path of the currect directory
34.17 ExeFileName() Function
Return the Ring executable file name
Syntax:
exefilename() ---> String contains the Ring executable file name
34.18 ChDir() Function
Change the current directory
Syntax:
ChDir(cNewPath)
34.19 ExeFolder() Function
Return the Ring executable file path
Syntax:
exefolder() ---> String contains the Ring executable path
34.20 Version() Function
Return the Ring version
Syntax:
version() ---> String contains the Ring version
Output:
34.16. CurrentDir() Function 265
Ring Documentation, Release 1.7
1.7
34.21 Shutdown() Function
Close the application
Syntax:
shutdown(nStatus) ---> Close the application
34.21. Shutdown() Function 266
CHAPTER
THIRTYFIVE
EVAL() AND DEBUGGING
In this chapter we are going to learn about
• Error Handling using Try/Catch/Done
• Eval() function
• Raise() function
• Assert() function
35.1 Try/Catch/Done
Syntax:
Try
Statements...
Catch
Statements...
Done
The statements in the Try block will be executed, if any error happens then the statements in the catch block will be
executed.
Inside the catch block we can use the variable cCatchError to get the error message
Example:
Try
see 5/0
Catch
see "Catch!" + nl + cCatchError
Done
Output:
Catch!
Error (R1) : Cann't divide by zero !
35.2 Eval() Function
We can execute code during the runtime from string using the Eval() function
Syntax:
267
Ring Documentation, Release 1.7
Eval(cCode)
Example:
Eval("nOutput = 5+2*5 " )
See "5+2*5 = " + nOutput + nl
Eval("for x = 1 to 10 see x + nl next")
Eval("func test see 'message from test!' ")
test()
Output:
5+2*5 = 15
1
2
3
4
5
6
7
8
9
10
message from test!
We can use the Return command to return a value
Example:
see Eval("return 5*5")
Output:
25
35.3 Raise() Function
We can raise an exception using the Raise() function
Syntax:
Raise(cErrorMessage)
The function will display the error message then end the execution of the program.
We can use Try/Catch/Done to avoid exceptions generated by raise() function.
Example:
nMode = 10
if nMode < 0 or nMode > 5
raise("Error : nMode not in the range 1:4")
ok
Output:
Line 4 Error : nMode not in the range 1:4
In raise in file testsraise.ring
35.3. Raise() Function 268
Ring Documentation, Release 1.7
Example:
try
testmode(6)
catch
see "avoid raise!"
done
testmode(-1)
func testmode nMode
if nMode < 0 or nMode > 5
raise("Error : nMode not in the range 1:4")
ok
Output:
avoid raise!
Line 12 Error : nMode not in the range 1:4
In raise In function testmode() in file testsraise2.ring
called from line 7 in file testsraise2.ring
35.4 Assert() Function
We can use the Assert() function to test conditions before executing the code
If the test fail the program will be terminated with an error message contains the assert condition.
Syntax:
Assert( condition )
Example:
x = 10
assert( x = 10)
assert( x = 100 )
Output:
Line 3 Assertion Failed!
In assert in file testsassert.ring
35.4. Assert() Function 269
CHAPTER
THIRTYSIX
DEMO PROGRAMS
In this chapter we will see simple demo programs
• Language Shell
• Main Menu
36.1 Language Shell
We can create simple interactive programming environment using the next program
while true
see nl + "code:> "
give cCode
try
eval(cCode)
catch
see cCatchError
done
end
Output:
code:> see "hello world"
hello world
code:> for x = 1 to 10 see x + nl next
1
2
3
4
5
6
7
8
9
10
code:> func test see "Hello from test" + nl
code:> test()
Hello from test
code:> bye
270
Ring Documentation, Release 1.7
36.2 Main Menu
Example:
# Demo Program
while true
see "
Main Menu
===========
[1] Say Hello
[2] Sum two numbers
[3] Stars
[4] Fact
[5] Exit
" give nMenu see nl
# we can use Switch-ON-Other-OFF instead of IF-BUT-ELSE-OK
Switch nMenu
On 1 sayhello()
On 2 Sum()
On 3 Stars()
On 4
see "Enter Number : " give x
see "Output : "
Try
see Fact(number(x))
Catch
see "Error in parameters!" + nl
Done
On "5" return
Other see "bad option" + nl
Off
end
func sayhello
see "Enter your name ? " give fname
see "Hello " + fname + nl
func sum
see "number 1 : " give num1 see "number 2 : " give num2
see "Sum : " see 0 + num1 + num2
func stars
for x = 1 to 10
see space(8)
for y = 1 to x see "*" next see nl
next
func fact x if x = 0 return 1 else return x * fact(x-1) ok
36.2. Main Menu 271

More Related Content

What's hot (20)

PDF
The Ring programming language version 1.5.2 book - Part 25 of 181
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.4 book - Part 26 of 185
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.7 book - Part 29 of 196
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.3 book - Part 26 of 184
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 34 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.3 book - Part 18 of 88
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.3 book - Part 25 of 184
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.1 book - Part 24 of 180
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.3 book - Part 23 of 88
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.4.1 book - Part 7 of 31
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.9 book - Part 34 of 210
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5 book - Part 5 of 31
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.4 book - Part 7 of 30
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.4 book - Part 32 of 185
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.4 book - Part 27 of 185
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 15 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.3 book - Part 27 of 184
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.2 book - Part 13 of 181
Mahmoud Samir Fayed
 
PDF
C++ course start
Net3lem
 
PDF
The Ring programming language version 1.8 book - Part 32 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 25 of 181
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 26 of 185
Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 29 of 196
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 26 of 184
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 34 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 18 of 88
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 25 of 184
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 24 of 180
Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 23 of 88
Mahmoud Samir Fayed
 
The Ring programming language version 1.4.1 book - Part 7 of 31
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 34 of 210
Mahmoud Samir Fayed
 
The Ring programming language version 1.5 book - Part 5 of 31
Mahmoud Samir Fayed
 
The Ring programming language version 1.4 book - Part 7 of 30
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 32 of 185
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 27 of 185
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 15 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 27 of 184
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 13 of 181
Mahmoud Samir Fayed
 
C++ course start
Net3lem
 
The Ring programming language version 1.8 book - Part 32 of 202
Mahmoud Samir Fayed
 

Similar to The Ring programming language version 1.7 book - Part 30 of 196 (15)

PDF
The Ring programming language version 1.6 book - Part 29 of 189
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.6 book - Part 36 of 189
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.8 book - Part 30 of 202
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.3 book - Part 25 of 88
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.6 book - Part 27 of 189
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.8 book - Part 39 of 202
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.1 book - Part 32 of 180
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.9 book - Part 27 of 210
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.6 book - Part 77 of 189
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.9 book - Part 31 of 210
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.4 book - Part 6 of 30
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.4 book - Part 5 of 30
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.9 book - Part 42 of 210
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.2 book - Part 77 of 181
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.7 book - Part 7 of 196
Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 29 of 189
Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 36 of 189
Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 30 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 25 of 88
Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 27 of 189
Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 39 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 32 of 180
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 27 of 210
Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 77 of 189
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 31 of 210
Mahmoud Samir Fayed
 
The Ring programming language version 1.4 book - Part 6 of 30
Mahmoud Samir Fayed
 
The Ring programming language version 1.4 book - Part 5 of 30
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 42 of 210
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 77 of 181
Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 7 of 196
Mahmoud Samir Fayed
 
Ad

More from Mahmoud Samir Fayed (20)

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

Recently uploaded (20)

PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
Digital Circuits, important subject in CS
contactparinay1
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 

The Ring programming language version 1.7 book - Part 30 of 196

  • 1. Ring Documentation, Release 1.7 34.7 IsMacOSX() Function We can check if the operating system is macOS or not using the IsMacOSX() function Syntax: IsMacOSX() ---> Returns 1 if the operating system is Mac OS X, Returns 0 if it's not 34.8 IsLinux() Function We can check if the operating system is Linux or not using the IsLinux() function Syntax: IsLinux() ---> Returns 1 if the operating system is Linux, Returns 0 if it's not 34.9 IsFreeBSD() Function We can check if the operating system is FreeBSD or not using the IsFreeBSD() function Syntax: IsFreeBSD() ---> Returns 1 if the operating system is FreeBSD, Returns 0 if it's not 34.10 IsAndroid() Function We can check if the operating system is Android or not using the IsAndroid() function Syntax: IsAndroid() ---> Returns 1 if the operating system is Android, Returns 0 if it's not 34.11 Example see "IsMSDOS() --> " + ismsdos() + nl see "IsWindows() --> " + iswindows() + nl see "IsWindows64() --> " + iswindows64() + nl see "IsUnix() --> " + isunix() + nl see "IsMacOSX() --> " + ismacosx() + nl see "IsLinux() --> " + islinux() + nl see "IsFreeBSD() --> " + isfreebsd() + nl see "IsAndroid() --> " + isandroid() + nl Output: IsMSDOS() --> 0 IsWindows() --> 1 IsWindows64() --> 0 IsUnix() --> 0 IsMacOSX() --> 0 34.7. IsMacOSX() Function 262
  • 2. Ring Documentation, Release 1.7 IsLinux() --> 0 IsFreeBSD() --> 0 IsAndroid() --> 0 34.12 Windowsnl() Function We can get the windows new line string using the Windowsnl() function. Syntax: WindowsNL() ---> Returns a string contains CR+LF = CHAR(13) + CHAR(10) Example: cStr = read("input.txt") if iswindows() cStr = substr(cStr,windowsnl(),nl) ok aList = str2list(cStr) # to do - list items processing using "for in" cStr = list2str(aList) if iswindows() cStr = substr(cStr,nl,windowsnl()) ok write("ouput.txt",cStr) 34.13 Get Command Line Arguments We can get the command line arguments passed to the ring script using the sysargv variable. The sysargv variable is a list contains the command line parameters. Example see copy("=",30) + nl see "Command Line Parameters" + nl see "Size : " + len(sysargv) + nl see sysargv see copy("=",30) + nl if len(sysargv) < 4 return ok nStart = sysargv[3] nEnd = sysargv[4] for x = nStart to nEnd see x + nl next Output b:mahmoudappsring>ring testssyspara.ring 1 10 ============================== Command Line Parameters 34.12. Windowsnl() Function 263
  • 3. Ring Documentation, Release 1.7 Size : 4 ring testssyspara.ring 1 10 ============================== 1 2 3 4 5 6 7 8 9 10 34.14 Get Active Source File Name We can get the active source file name (*.ring) using the filename() function Syntax: filename() ---> String contains the active source file name. Example: see "Active Source File Name : " + filename() + nl Output: Active Source File Name : testsfilename.ring Example: if sysargv[2] = filename() see "I'm the main program file!" + nl # we can run tests here! else see "I'm a sub file in a program" + nl ok 34.15 PrevFileName() Function Using the PrevFileName() function we can get the previous active source file name. The previous file would be the file of the caller function, Or the file of the function that we called before calling PrevFileName(). Syntax: prevfilename() ---> String contains the previous source file name. Example: The next function in stdlib.ring uses the PrevFileName() to know if the file of the caller function is the main source file of the program or not. 34.14. Get Active Source File Name 264
  • 4. Ring Documentation, Release 1.7 Func IsMainSourceFile if PrevFileName() = sysargv[2] return true ok return false 34.16 CurrentDir() Function Return the path of the current directory Syntax: CurrenDir() ---> String contains the path of the currect directory 34.17 ExeFileName() Function Return the Ring executable file name Syntax: exefilename() ---> String contains the Ring executable file name 34.18 ChDir() Function Change the current directory Syntax: ChDir(cNewPath) 34.19 ExeFolder() Function Return the Ring executable file path Syntax: exefolder() ---> String contains the Ring executable path 34.20 Version() Function Return the Ring version Syntax: version() ---> String contains the Ring version Output: 34.16. CurrentDir() Function 265
  • 5. Ring Documentation, Release 1.7 1.7 34.21 Shutdown() Function Close the application Syntax: shutdown(nStatus) ---> Close the application 34.21. Shutdown() Function 266
  • 6. CHAPTER THIRTYFIVE EVAL() AND DEBUGGING In this chapter we are going to learn about • Error Handling using Try/Catch/Done • Eval() function • Raise() function • Assert() function 35.1 Try/Catch/Done Syntax: Try Statements... Catch Statements... Done The statements in the Try block will be executed, if any error happens then the statements in the catch block will be executed. Inside the catch block we can use the variable cCatchError to get the error message Example: Try see 5/0 Catch see "Catch!" + nl + cCatchError Done Output: Catch! Error (R1) : Cann't divide by zero ! 35.2 Eval() Function We can execute code during the runtime from string using the Eval() function Syntax: 267
  • 7. Ring Documentation, Release 1.7 Eval(cCode) Example: Eval("nOutput = 5+2*5 " ) See "5+2*5 = " + nOutput + nl Eval("for x = 1 to 10 see x + nl next") Eval("func test see 'message from test!' ") test() Output: 5+2*5 = 15 1 2 3 4 5 6 7 8 9 10 message from test! We can use the Return command to return a value Example: see Eval("return 5*5") Output: 25 35.3 Raise() Function We can raise an exception using the Raise() function Syntax: Raise(cErrorMessage) The function will display the error message then end the execution of the program. We can use Try/Catch/Done to avoid exceptions generated by raise() function. Example: nMode = 10 if nMode < 0 or nMode > 5 raise("Error : nMode not in the range 1:4") ok Output: Line 4 Error : nMode not in the range 1:4 In raise in file testsraise.ring 35.3. Raise() Function 268
  • 8. Ring Documentation, Release 1.7 Example: try testmode(6) catch see "avoid raise!" done testmode(-1) func testmode nMode if nMode < 0 or nMode > 5 raise("Error : nMode not in the range 1:4") ok Output: avoid raise! Line 12 Error : nMode not in the range 1:4 In raise In function testmode() in file testsraise2.ring called from line 7 in file testsraise2.ring 35.4 Assert() Function We can use the Assert() function to test conditions before executing the code If the test fail the program will be terminated with an error message contains the assert condition. Syntax: Assert( condition ) Example: x = 10 assert( x = 10) assert( x = 100 ) Output: Line 3 Assertion Failed! In assert in file testsassert.ring 35.4. Assert() Function 269
  • 9. CHAPTER THIRTYSIX DEMO PROGRAMS In this chapter we will see simple demo programs • Language Shell • Main Menu 36.1 Language Shell We can create simple interactive programming environment using the next program while true see nl + "code:> " give cCode try eval(cCode) catch see cCatchError done end Output: code:> see "hello world" hello world code:> for x = 1 to 10 see x + nl next 1 2 3 4 5 6 7 8 9 10 code:> func test see "Hello from test" + nl code:> test() Hello from test code:> bye 270
  • 10. Ring Documentation, Release 1.7 36.2 Main Menu Example: # Demo Program while true see " Main Menu =========== [1] Say Hello [2] Sum two numbers [3] Stars [4] Fact [5] Exit " give nMenu see nl # we can use Switch-ON-Other-OFF instead of IF-BUT-ELSE-OK Switch nMenu On 1 sayhello() On 2 Sum() On 3 Stars() On 4 see "Enter Number : " give x see "Output : " Try see Fact(number(x)) Catch see "Error in parameters!" + nl Done On "5" return Other see "bad option" + nl Off end func sayhello see "Enter your name ? " give fname see "Hello " + fname + nl func sum see "number 1 : " give num1 see "number 2 : " give num2 see "Sum : " see 0 + num1 + num2 func stars for x = 1 to 10 see space(8) for y = 1 to x see "*" next see nl next func fact x if x = 0 return 1 else return x * fact(x-1) ok 36.2. Main Menu 271