You are on page 1of 10

Ring Documentation, Release 1.

Method Description/Output
read(cFileName) Read the file content
write(cFileName,cStr) Write string to file
dir(cFolderPath) Get the folder contents (files & sub folders)
rename(cOld,cNew) Rename files using the Rename() function
remove(cFileName) Delete a file using the Remove() function
open(cFileName,cMode) Open a file using the Fopen() function
close() Close file
flush() Flushes the output buffer of a stream
reopen(cFileName,cMode) Open another file using the same file handle
tempfile() Creates a temp. file (binary).
seek(noffset,nwhence) Set the file position of the stream
tell() Know the current file position of a stream
rewind() Set the file position to the beginning of the file
getpos() Get handle to the current file position
setpos(poshandle) Set the current file position
clearerr() Clear the EOF error and the error indicators of a stream
eof() Test the end-of-file indicator
error() Test the error indicator
perror(cErrorMessage) Print error message to the stderr
getc() Get the next character from the stream
gets(nsize) Read new line from the stream
putc(cchar) Write a character to the stream
puts(cStr) Write a string to the stream
ungetc(cchar) Push a character to the stream
fread(nsize) Read data from a stream
fwrite(cString) Write data to a stream
exists(cFileName) Check if a file exists
example:
Load "stdlib.ring"

ofile = new file

See "Test the file Class Methods" + nl


see ofile.read(filename())

see nl
ofile.open(filename(),"r")
see ofile.gets(100) + nl
ofile.close()

38.11 System Class

Methods:

38.11. System Class 230


Ring Documentation, Release 1.2

Method Description/Output
system() Execute system commands
sysget() Get environment variables
ismsdos() Check if the operating system is MSDOS or not
iswindows() Check if the operating system is Windows or not
iswindows64() Check if the operating system is Windows 64bit or not
isunix() Check if the operating system is Unix or not
ismacosx() Check if the operating system is Mac OS X or not
islinux() Check if the operating system is Linux or not
isfreebsd() Check if the operating system is FreeBSD or not
isandroid() Check if the operating system is Android or not
windowsnl() Get the windows new line string
sysargv() Get the command line arguments passed to the ring script
filename() Get the active source file
example:
Load "stdlib.ring"

oSystem = new System

See "Test the System Class Methods" + nl

oSystem.system("dir")
see oSystem.sysget("path") + nl
see oSystem.ismsdos() + nl
see oSystem.iswindows() + nl
see oSystem.iswindows64() + nl
see oSystem.isunix() + nl
see oSystem.ismacosx() + nl
see oSystem.islinux() + nl
see oSystem.isfreebsd() + nl
see oSystem.isandroid() + nl
see oSystem.windowsnl() + nl
see oSystem.sysargv() + nl
see oSystem.filename() + nl

38.12 Debug Class

Methods:
Method Description/Output
eval(cCode) Execute code during the runtime from string.
raise(cError) Raise an exception.
assert(cCondition) Test condition before executing the code.
example:
Load "stdlib.ring"

oDebug = new Debug


See "Test the Debug Class Methods" + nl
oDebug.eval("see 'Hello'+nl")
try
x = 10
oDebug.assert(x=11)

38.12. Debug Class 231


Ring Documentation, Release 1.2

catch see "assert" + nl done


raise("Error!")

38.13 DataType Class

Methods:
Method Description/Output
isstring(vValue) We can know if the value is a string or not.
isnumber(vValue) We can know if the value is a number or not.
islist(vValue) We can know if the value is a list or not.
type(vValue) Know the type of a value
isnull(vValue) Check the value to know if its null or not.
isalnum(vValue) 1 if the value is digit/letter or 0 if not
isalpha(vValue) 1 if the value is a letter or 0 if not
iscntrl(vValue) 1 if the value is a control character (no printing position)
isdigit(vValue) 1 if the value is a digit or 0 if not
isgraph(vValue) 1 if the value can be printed (Except space) or 0 if not
islower(vValue) 1 if the value is lowercase letter or 0 if not
isprint(vValue) 1 if the value occupies a printing position or 0 if not
ispunct(vValue) 1 if the value is a punctuation character or 0 if not
isspace(vValue) 1 if the value is a white-space or 0 if not
isupper(vValue) 1 if the value is an uppercase alphabetic letter or 0 if not
isxdigit(vValue) 1 if the value is a hexdecimal digit character or 0 if not
example:
Load "stdlib.ring"

oDataType = new DataType


See "Test the DataType Class Methods" + nl
see oDataType.isstring("test") + nl
see oDataType.isnumber(1) + nl
see oDataType.islist(1:3) + nl
see oDataType.type("test") + nl
see oDataType.isnull(null) + nl
see oDataType.isalnum("Hello") + nl + # print 1
oDataType.isalnum("123456") + nl + # print 1
oDataType.isalnum("ABCabc123") + nl + # print 1
oDataType.isalnum("How are you") + nl # print 0 because of spaces
see oDataType.isalpha("Hello") + nl + # print 1
oDataType.isalpha("123456") + nl + # print 0
oDataType.isalpha("ABCabc123") + nl + # print 0
oDataType.isalpha("How are you") + nl # print 0
See oDataType.iscntrl("hello") + nl + # print 0
oDataType.iscntrl(nl) # print 1
see oDataType.isdigit("0123456789") + nl + # print 1
oDataType.isdigit("0123a") + nl
see oDataType.isgraph("abcdef") + nl + # print 1
oDataType.isgraph("abc def") + nl # print 0
see oDataType.islower("abcDEF") + nl + # print 0
oDataType.islower("ghi") + nl # print 1
see oDataType.isprint("Hello") + nl + # print 1
oDataType.isprint("Nice to see you") + nl + # print 1
oDataType.isprint(nl) + nl # print 0

38.13. DataType Class 232


Ring Documentation, Release 1.2

see oDataType.isprint("Hello") + nl # print 1


see oDataType.isupper("welcome") + nl + # print 0
oDataType.isupper("WELCOME") + nl # print 1
see oDataType.isxdigit("0123456789abcdef") + nl + # print 1
oDataType.isxdigit("123z") # print 0

Output:
Test the DataType Class Methods
1
1
1
STRING
1
1
1
1
0
1
0
0
0
0
11
0
1
0
0
1
1
1
0
1
0
1
1
0

38.14 Conversion Class

Methods:
Method Description/Output
number(vValue) Convert strings to numbers.
string(vValue) Convert numbers to strings.
ascii(vValue) Get the ASCII code for a letter.
char(vValue) Convert the ASCII code to character.
hex(vValue) Convert decimal to hexadecimal.
dec(vValue) Convert hexadecimal to decimal.
str2hex(vValue) Convert string characters to hexadecimal characters.
hex2str(vValue) Convert hexadecimal characters to string.
example:
Load "stdlib.ring"

38.14. Conversion Class 233


Ring Documentation, Release 1.2

oConversion = new conversion


See "Test the conversion Class Methods" + nl
See oConversion.number("3") + 5 + nl
See oConversion.string(3) + "5" + nl
See oConversion.Ascii("m") + nl
See oConversion.char(77) + nl
see oConversion.hex(162) + nl
see oConversion.dec("a2") + nl
cHex = oConversion.str2hex("Hello")
see cHex + nl
see oConversion.hex2str(cHex) + nl

Output:
Test the conversion Class Methods
8
35
109
M
a2
162
48656c6c6f
Hello

38.15 ODBC Class

Methods:
Method Description/Output
drivers() Get a list of ODBC drivers.
datasources() Get a list of ODBC data sources.
close() Free resources.
connect(cConString) Connect to the database.
disconnect() Close the connection.
execute(cSQL) Execute SQL Statements
colcount() Get columns count in the query result
fetch() Fetch a row from the query result
getdata(nCol) Get column value from the fetched row
tables() Get a list of tables inside the database
columns(cTableName) Get a list of columns inside the table
autocommit(lStatus) Enable or disable the auto commit feature
commit() Commit updates to the database
rollback() Rollback updates to the database
example:
Load "stdlib.ring"

oodbc = new odbc


See "Test the odbc Class Methods" + nl
oODBC {
see drivers()
see datasources()
See "Connect to database" + nl
see connect("DBQ=test.mdb;Driver={Microsoft Access Driver (*.mdb)}") + nl
See "Select data" + nl

38.15. ODBC Class 234


Ring Documentation, Release 1.2

see execute("select * from person") + nl


nMax = colcount()
See "Columns Count : " + nMax + nl
while fetch()
See "Row data:" + nl
for x = 1 to nMax
see getdata(x) + " - "
next
end
See "Close database..." + nl
disconnect()
close()
}

38.16 MySQL Class

Methods:
Method Description/Output
info() Return string contains the MySQL Client version.
error() Get the error message from the MySQL Client.
connect(cServer,cUser,cPass,cDatabase) Connect to the MySQL database server.
close() Close the connection to the MySQL database.
query(cQuery) Execute SQL queries.
insert_id() Get the inserted row id.
result() Get the query result (data without column names).
next_result() Move to the next query result.
columns() Get a list of columns names.
result2() Get all of the column names then the query result in one list.
escape_string(cStr) Before storing binary data and special characters in the database.
autocommit(lStatus) Enable or disable the auto commit feature.
commit() Commit updates to the database.
rollback() Rollback updates to the database.
example:
Load "stdlib.ring"

omysql = new mysql


See "Test the MySQL Class Methods" + nl
omysql {
see info() + nl
connect("localhost", "root", "root","mahdb")
see "Execute Query" + nl
query("SELECT * FROM Employee")
see "Print Result" + nl
see result2()
see "Close database" + nl
close()
}

Output:
Test the MySQL Class Methods
5.5.30

38.16. MySQL Class 235


Ring Documentation, Release 1.2

Execute Query
Print Result
Id
Name
Salary
1
Mahmoud
15000
2
Samir
16000
3
Fayed
17000
Close database

38.17 SQLite Class

Methods:
Method Description/Output
open(cDatabase) Open Database.
close() Close Database.
errormessage() Get Error Message.
execute(cSQL) Execute Query.
example:
Load "stdlib.ring"

osqlite = new sqlite


See "Test the sqlite Class Methods" + nl
osqlite {
open("test.db")
sql = "CREATE TABLE COMPANY(" +
"ID INT PRIMARY KEY NOT NULL," +
"NAME TEXT NOT NULL," +
"AGE INT NOT NULL," +
"ADDRESS CHAR(50)," +
"SALARY REAL );"

execute(sql)

sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " +


"VALUES (1, 'Mahmoud', 29, 'Jeddah', 20000.00 ); " +
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " +
"VALUES (2, 'Ahmed', 27, 'Jeddah', 15000.00 ); " +
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" +
"VALUES (3, 'Mohammed', 31, 'Egypt', 20000.00 );" +
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" +
"VALUES (4, 'Ibrahim', 24, 'Egypt ', 65000.00 );"

execute(sql)

aResult = execute("select * from COMPANY")


for x in aResult
for t in x

38.17. SQLite Class 236


Ring Documentation, Release 1.2

see t[2] + nl
next
next
see copy("*",50) + nl
for x in aResult
see x["name"] + nl
next
close()
}

Output:
Test the sqlite Class Methods
1
Mahmoud
29
Jeddah
20000.0
2
Ahmed
27
Jeddah
15000.0
3
Mohammed
31
Egypt
20000.0
4
Ibrahim
24
Egypt
65000.0
**************************************************
Mahmoud
Ahmed
Mohammed
Ibrahim

38.18 Security Class

Methods:
Method Description/Output
md5(cString) Calculate the MD5 hash.
sha1(cString) Calculate the SHA1 hash.
sha256(cString) Calculate the SHA256 hash.
sha512(cString) Calculate the SHA512 hash.
sha384(cString) Calculate the SHA384 hash.
sha224(cString) Calculate the SHA224 hash.
encrypt(cString,cKey,cIV) Cncrypts the data using the Blowfish algorithm.
decrypt(cString,cKey,cIV) Decrypt the data encrypted using the Encrypt() method.
randbytes(nSize) Generate a string of pseudo-random bytes.
example:

38.18. Security Class 237


Ring Documentation, Release 1.2

Load "stdlib.ring"

osecuirty = new secuirty


See "Test the secuirty Class Methods" + nl
oSecuirty {
see md5("hello") + nl +
sha1("hello") + nl + sha256("hello") + nl +
sha512("hello") + nl + sha384("hello") + nl +
sha256("hello") + nl
list = 0:15 cKey="" for x in list cKey += char(x) next
list = 1:8 cIV = "" for x in list cIV += char(x) next
cCipher = encrypt("hello",cKey,cIV)
see cCipher + nl + decrypt(cCipher,cKey,cIV) + nl
}

38.19 Internet Class

Methods:
download(cURL)
sendemail(cSMTPServer,cEmail,cPassword,cSender,cReceiver,cCC,cTitle,cContent)
example:
Load "stdlib.ring"

ointernet = new internet


See "Test the internet Class Methods" + nl
ointernet {
see download("www.ring-lang.sf.net")
}

38.19. Internet Class 238


CHAPTER

THIRTYNINE

DECLARATIVE PROGRAMMING USING NESTED STRUCTURES

In this chapter we are going to learn how to build declarative programming world using nested structures on the top of
object oriented.
We will learn about
Creating Objects inside Lists
Composition and Returning Objects and Lists by Reference
Executing code after the end of object access
Declarative Programming on the top of Object-Oriented

39.1 Creating Objects inside Lists

We can create objects inside lists during list definition. Also we can add objects to the list at any time using the Add()
function or the + operator.
Example:
alist = [new point, new point, new point] # create list contains three objects

alist + [1,2,3] # add another item to the list

see "Item 4 is a list contains 3 items" + nl


see alist[4]

add(alist , new point)


alist + new point

alist[5] { x = 100 y = 200 z = 300 }


alist[6] { x = 50 y = 150 z = 250 }

see "Object inside item 5" + nl


see alist[5]
see "Object inside item 6" + nl
see alist[6]

class point x y z

Output:
Item 4 is a list contains 3 items
1
2

239

You might also like