Vous êtes sur la page 1sur 10

Ring Documentation, Release 1.

title
nWindowTitle = 0
# Keywords to ignore, just give them any value
the=0

ChangeRingKeyword and _and


and=0
ChangeRingKeyword _and and

func geti
if nIwantwindow = 0
nIwantwindow++
ok

func getwant
if nIwantwindow = 1
nIwantwindow++
ok

func getwindow
if nIwantwindow = 2
nIwantwindow= 0
see "Instruction : I want window" + nl
ok
if nWindowTitle = 0
nWindowTitle++
ok

func settitle cValue


if nWindowTitle = 1
nWindowTitle=0
see "Instruction : Window Title = " + cValue + nl
ok

40.7 BraceStart and BraceEnd Methods

We can write code that will be executed before/after using { }


Example:
o1 = new test {
see "Hello" + nl
}

o1 {}

class test

func bracestart
see "start" + nl

func braceend
see "end" + nl

Output:

40.7. BraceStart and BraceEnd Methods 250


Ring Documentation, Release 1.2

start
Hello
end
start
end

40.8 BraceExprEval Method

The next example demonstrates how to use the BraceExprEval method to get expressions in Natural code.
Example:
new natural {
create 5
}

class natural
create=0
lkeyword = false
func braceexpreval r
if lkeyword lkeyword=false return ok
see "expr eval" + nl
see "type: " + type(r) see nl
see "value : " see r see nl
func getcreate
lkeyword = true
see "create" + nl

Output:
create
expr eval
type: NUMBER
value : 5

40.9 Real Natural Code

The next example is a more advanced example


# Natural Code
new program {
Accept 2 numbers then print the sum
}

# Natural Code Implementation


class program
# Keywords
Accept=0 numbers=0 then=0 print=0 the=0 sum=0

# Execution
func braceexpreval x
value = x
func getnumbers
for x=1 to value
see "Enter Number ("+x+") :" give nNumber

40.8. BraceExprEval Method 251


Ring Documentation, Release 1.2

aNumbers + nNumber
next
func getsum
nSUm = 0
for x in aNumbers nSum+= x next
see "The Sum : " + nSum
private
value=0 aNumbers=[]

Output:
Enter Number (1) :3
Enter Number (2) :4
The Sum : 7

40.10 BraceError() Method

The next examples demonstrates how to use the BraceError method to handle errors when accessing the object using
braces {}.
Example:
func main
o1 = new point {
x=10 y=20 z=30
TEST
SEE test
}

class point x y z
func braceerror
see "Handle Error!" + nl
SEE "Message :" + cCatchError + nl
if ( left(cCatchError,11) = "Error (R24)" ) and not isattribute(self,"test")
see "add attribute" + nl
addattribute(self,"test")
test = 10
ok
see "done" + nl
return

Output:
Handle Error!
Message :Error (R24) : Using uninitialized variable : test
add attribute
done
10

Example:
new point {
x=10 y=20 z=30
test()
see "mmm..." + NL
}

40.10. BraceError() Method 252


Ring Documentation, Release 1.2

class point x y z
func braceerror
see "Handle Error!" + nl
see "Message :" + cCatchError + nl
see self
see "Done" + NL

Output:
Handle Error!
Message :Error (R3) : Calling Function without definition !: test
x: 10.000000
y: 20.000000
z: 30.000000
Done
mmm...

40.11 Clean Natural Code

Instead of typing the literal as literal we can accept the words directly.
Example:
The next example accept hello world instead of hello world
ChangeRingKeyword and _and
ChangeRingOperator = is

New App
{
I want window and the window title is hello world
}

ChangeRingOperator is =

Class App

# Attributes for the instruction I want window


i want window
nIwantwindow = 0
# Attributes for the instruction Window title
# Here we don't define the window attribute again
title
nWindowTitle = 0
# Keywords to ignore, just give them any value
the=0 and=0
# Data
literal = ""

ChangeRingKeyword _and and

func geti
if nIwantwindow = 0
nIwantwindow++
ok

func getwant

40.11. Clean Natural Code 253


Ring Documentation, Release 1.2

if nIwantwindow = 1
nIwantwindow++
ok

func getwindow
if nIwantwindow = 2
nIwantwindow= 0
see "Instruction : I want window" + nl
ok
if nWindowTitle = 0
nWindowTitle++
ok

func settitle cValue


if nWindowTitle = 1
nWindowTitle=2
ok

func braceend
if nWindowTitle = 2
see "Instruction : Window Title = " + literal + nl
nWindowTitle = 0
ok

func braceerror
c= substr(cCatchError,":")
while c > 0
c= substr(cCatchError,":")
cCatchError=substr(cCatchError,c+1)
end
literal += substr(cCatchError,1)

40.11. Clean Natural Code 254


CHAPTER

FORTYONE

WEB DEVELOPMENT (CGI LIBRARY)

In this chapter we will learn about developing Web applications using a CGI Library written in the Ring language.

41.1 Configure the Apache web server

We can use Ring with any web server that support CGI. In this section we will learn about using Ring with the Apache
HTTP Server.
You can download Apache from : http://httpd.apache.org/
Or you can get it included with other projects like
XAMPP : https://www.apachefriends.org/download.html
Install then open the file:
xampp\apache\conf\httpd.conf

search for
<Directory />

Then after it add


Options FollowSymLinks +ExecCGI

So we have
<Directory />
Options FollowSymLinks +ExecCGI

Search for the next line and be sure that its not commented
LoadModule cgi_module modules/mod_cgi.so

Search for : AddHandler cgi-script


Then add .ring to the supported cgi extensions
Example
AddHandler cgi-script .cgi .ring

Example
AddHandler cgi-script .cgi .pl .asp .ring

255
Ring Documentation, Release 1.2

Run/Start the server


Create your web applications in a directory supported by the web server.
Example:
Apache2.2\htdocs\mywebapplicationfolder

Example:
xampp\htdocs\mywebapplicationfolder

Inside the source code file (*.ring), Add this line


#!c:\ring\bin\ring.exe -cgi

Note: Change the previous line based on the path to ring.exe in your machine

41.2 Ring CGI Hello World Program

The next program is the Hello World program


#!c:\ring\bin\ring.exe -cgi

See "content-type : text/html" +nl+nl+


"Hello World!" + nl

41.3 Hello World Program using the Web Library

We can use the web library to write CGI Web applications quickly
Example (1) :
#!c:\ring\bin\ring.exe -cgi

Load "weblib.ring"

Import System.Web

New Page
{
Text("Hello World!")
}

Example (2) :
#!c:\ring\bin\ring.exe -cgi

Load "weblib.ring"

Import System.Web

WebPage()
{
Text("Hello World!")
}

41.2. Ring CGI Hello World Program 256


Ring Documentation, Release 1.2

Tip: the difference between ex. 1 and ex. 2 is using WebPage() function to return the page object instead of creating
the object using new statement.

41.4 Web Library Features

The next features are provided by the Web library to quickly create web applications.
Generate HTML pages using functions
Generate HTML pages using objects
HTTP Get
HTTP Post
Files Upload
URL Encode
Templates
CRUD MVC Sample
Users Logic & Registration Sample

41.5 HTTP Get Example

The Page User Interface


#!c:\ring\bin\ring.exe -cgi
Load "weblib.ring"
Import System.Web
New Page
{
Title = "Test HTTP Get"
divstart([ :style = StyleSizeFull() ] )
boxstart()
text( "Test HTTP GET" )
newline()
boxend()
divstart([ :style = Styledivcenter("600px","550px") +
StyleGradient(21) ])
divstart([:style = stylefloatleft() + stylesize("100px","100%") +
stylecolor("black") + stylegradient(58)])
formstart("ex5.ring")
tablestart([ :style = stylesize("65%","90%") +
stylemarginleft("35%") +
stylemargintop("30%") ])
rowstart([])
cellstart([])
text ( "Name : " )
cellend()
cellstart([])
cTextboxStyle = StyleMarginLeft("5%") +
StyleWidth("250px") +
StyleColor("black") +

41.4. Web Library Features 257


Ring Documentation, Release 1.2

StyleBackColor("white")
textbox([ :name = "Name", :style = cTextboxStyle ] )
cellend()
rowend()
rowstart([])
cellstart([])
text ( "Address : " )
cellend()
cellstart([])
textbox([ :name = "Address", :style = cTextboxStyle] )
cellend()
rowend()
rowstart([])
cellstart([])
text ( "Phone : " )
cellend()
cellstart([])
textbox([ :name = "Phone", :style = cTextboxStyle ])
cellend()
rowend()
rowstart([])
cellstart([])
text ( "Age : " )
cellend()
cellstart([])
textbox([ :name = "Age", :style = cTextboxStyle ])
cellend()
rowend()
rowstart([])
cellstart([])
text ( "City: " )
cellend()
cellstart([])
listbox([ :name = "City", :items = ["Cairo","Riyadh","Jeddah"],
:style = stylemarginleft("5%") + stylewidth("400px") ] )
cellend()
rowend()
rowstart([])
cellstart([])
text ( "Country : " )
cellend()
cellstart([])
combobox([ :name = "Country",
:items = ["Egypt","Saudi Arabia","USA"],
:style = stylemarginleft("5%") +
stylewidth("400px")+
stylecolor("black")+
stylebackcolor("white")+
stylefontsize("14px") ])
cellend()
rowend()
rowstart([])
cellstart([])
text ( "Note : " )
cellend()
cellstart([])
editbox([ :name = "Notes",
:style = stylemarginleft("5%") +

41.5. HTTP Get Example 258


Ring Documentation, Release 1.2

stylesize("400px","100px")+
stylecolor("black")+
stylebackcolor("white") ,
:value = "write comments here..." ] )
cellend()
rowend()
rowstart([])
cellstart([])
cellend()
cellstart([])
submit([ :value = "Send" , :Style = stylemarginleft("5%") ])
cellend()
rowend()
tableend()
formend()
divend()
divend()
divend()
}

Screen Shot:

41.5. HTTP Get Example 259

Vous aimerez peut-être aussi