You are on page 1of 10

Ring Documentation, Release 1.

42.16 objectid() Function

We can know the object id using the objectid() function


Syntax:
objectid(object) --> Returns the object id

Example:
o1 = new point
see objectid(o1) + nl
test(o1)

func test v
see objectid(v) + nl

Class point x y z

Output:
021B5808
021B5808

42.17 isobject() Function

We can check the variable to know if its an object or not using the isobject() function
Syntax:
isobject(variable) --> Returns True if it's an object, False if it's not

42.18 attributes() Function

We can get the object attributes using the attributes() function


Syntax:
attributes(object) --> Returns a list contains the object attributes

Example:
o1 = new point
aList = attributes(o1) # we can use see attributes(o1)
for t in aList see t next # print xyz
Class Point x y z

42.19 methods() Function

We can get the object methods using the methods() function


Syntax:

42.16. objectid() Function 275


Ring Documentation, Release 1.5

methods(object) --> Returns a list contains the object methods

Example:
o1 = new test
aList = methods(o1)

for x in aList
cCode = "o1."+x+"()"
eval(cCode)
next

Class Test
func f1
see "hello from f1" + nl
func f2
see "hello from f2" + nl
func f3
see "hello from f3" + nl
func f4
see "hello from f4" + nl

Output:
hello from f1
hello from f2
hello from f3
hello from f4

42.20 isattribute() Function

We can test if the object contains an attribute or not using the isattribute() function
Syntax:
isattribute(object,cAttributeName) --> Returns True if the object contains the attribute

Example:
o1 = new point

see isattribute(o1,"x") + nl # print 1


see isattribute(o1,"t") + nl # print 0
see isattribute(o1,"y") + nl # print 1
see isattribute(o1,"z") + nl # print 1

class point x y z

42.21 isprivateattribute() Function

We can test if the object contains a private attribute or not using the isprivateattribute() function
Syntax:

42.20. isattribute() Function 276


Ring Documentation, Release 1.5

isprivateattribute(object,cAttributeName) --> Returns True if the object


contains the private attribute

Example:
o1 = new person

see isprivateattribute(o1,"name") + nl +
isprivateattribute(o1,"address") + nl +
isprivateattribute(o1,"phone") + nl +
isprivateattribute(o1,"job") + nl +
isprivateattribute(o1,"salary")

Class Person
name address phone
private
job salary

Output:
0
0
0
1
1

42.22 ismethod() Function

We can test if the object class contains a method or not using the ismethod() function
Syntax:
ismethod(object,cMethodName) --> Returns True if the object class contains the method

Example:
o1 = new point

see ismethod(o1,"print") + nl # print 1

mylist = []
mylist + new point

see ismethod(mylist[1],"print") + nl # print 1

class point x y z
func print
see x + nl + y + nl + z + nl

42.23 isprivatemethod() Function

We can test if the object class contains a private method or not using the isprivatemethod() function
Syntax:

42.22. ismethod() Function 277


Ring Documentation, Release 1.5

isprivatemethod(object,cMethodName) --> Returns True if the object class contains


the private method

Example:
o1 = new Test

see isprivatemethod(o1,"f1") + nl +
isprivatemethod(o1,"f2")

Class Test
func f1
see "message from f1()" + nl
private
func f2
see "message from f2()" + nl

Output:
0
1

42.24 addattribute() Function

We can add an attribute (or a group of attributes) to the object state (not the class) using the addattribute() function
Syntax:
AddAttribute(object,cAttributeName|aAttributesList)

Example(1):
see new point {x=10 y=20 z=30}
Class Point
AddAttribute(self,["x","y","z"])

Example(2):
o1 = new point
addattribute(o1,"x")
addattribute(o1,"y")
addattribute(o1,"z")
see o1 {x=10 y=20 z=30}
class point

Output:
x: 10.000000
y: 20.000000
z: 30.000000

42.25 addmethod() Function

We can add a method to the object class using the addmethod() function This method can be used with any object from
the same class.

42.24. addattribute() Function 278


Ring Documentation, Release 1.5

Syntax:
AddMethod(Object,cNewMethodName,cMethodName|AnonymousFunction)

Example:
o1 = new point { x=10 y=20 z=30 }

addmethod(o1,"print", func { see x + nl + y + nl + z + nl } )

o1.print()

Class point
x y z

Output:
10
20
30

Instead of using anonymous function to add new method to the class, we can use the function name
Example:
o1 = new point { x=10 y=20 z=30 }

myfunc = func { see x + nl + y + nl + z + nl }

addmethod(o1,"print", myfunc )
addmethod(o1,"display", myfunc )
addmethod(o1,"show", myfunc )

o1.print()
o1.display()
o1.show()

Class point
x y z

Output:
10
20
30
10
20
30
10
20
30

Since we add the method to the class, any object from that class can use this method
Example:
o1 = new point { x=10 y=20 z=30 }
o2 = new point { x=100 y=200 z=300 }
o3 = new point { x=50 y=150 z=250 }

addmethod(o1,"print", func { see x + nl + y + nl + z + nl } )

42.25. addmethod() Function 279


Ring Documentation, Release 1.5

o1.print()
o2.print()
o3.print()

Class point
x y z

Output:
10
20
30
100
200
300
50
150
250

42.26 getattribute() function

We can get the object attribute value using the getattribute() function
Syntax:
GetAttribute(oObject,cAttributeName) ---> Attribute Value

Example:
o1 = new point

see getattribute(o1,"name") + nl +
getattribute(o1,"x") + nl +
getattribute(o1,"y") + nl +
getattribute(o1,"z") + nl

Class Point
x=10 y=20 z=30
name = "3D-Point"

Output:
3D-Point
10
20
30

Example:
We can Find a Class List Member using GetAttribute() using a function findclass() The Find uses the member name,
rather than the column number
myList =
[new Company {position=3 name="Mahmoud" symbol="MHD"},
new Company {position=2 name="Bert" symbol="BRT"},
new Company {position=1 name="Ring" symbol="RNG"}
]

42.26. getattribute() function 280


Ring Documentation, Release 1.5

see myList
see nl +"=====================" + nl + nl

for i = 1 to len(myList)
see "Pos: "+ i +" | "+ myList[i].position +" | "+ myList[i].name +
" | "+ myList[i].symbol +" | "+ nl
next

See findclass(myList, "MHD", "symbol") +nl ### Specify Member class name

###---------------------------------------

func findclass classList, cValue, classMember

See nl + "FindClass: " +" "+ cValue + nl + nl

for i = 1 to len(classList)
result = getattribute( classList[i], classMember )

See "Result-Attr: " + i +" "+ result +nl


if result = cValue
j = i
ok
next
return j

###--------------------------------------

class company position name symbol

Output:
Pos: 1 | 3 | Mahmoud | MHD |
Pos: 2 | 2 | Bert | BRT |
Pos: 3 | 1 | Ring | RNG |

FindClass: MHD

Result-Attr: 1 MHD
Result-Attr: 2 BRT
Result-Attr: 3 RNG

42.27 setattribute() function

We can set the object attribute value using the setattribute() function
Syntax:
SetAttribute(oObject,cAttributeName,Value)

Example:
o1 = new person
setattribute(o1,"cName","Mahmoud")

42.27. setattribute() function 281


Ring Documentation, Release 1.5

setattribute(o1,"nSalary",1000000)
setattribute(o1,"aColors",["white","blue","yellow"])

see o1
see o1.aColors

Class Person
cName
nSalary
aColors

Output:
cname: Mahmoud
nsalary: 1000000.000000
acolors: List...
white
blue
yellow

42.28 mergemethods() Function

We can share methods between classes without inheritance using the MergeMethods() function
This function merge class methods to another class.
Syntax:
MergeMethods(cClassNameDestination,cClassNameSource)

Example:
mergemethods("count","share")
mergemethods("count2","share")

o1 = new count { test() }


o1 = new count2 { test() }

Class Share
func one
see "one" + nl
func two
see "two" + nl
func three
see "three" + nl

Class Display
Func printline
see copy("*",20) + nl

Class Count from Display


Func test
printline()
one()
two()
three()
printline()

42.28. mergemethods() Function 282


Ring Documentation, Release 1.5

Class Count2 from Display


Func test
three()
two()
one()
printline()

Output:
********************
one
two
three
********************
three
two
one
********************

42.29 packagename() Function

We can know the package name of the latest sucessful import command using the packagename() function
Syntax:
packagename() --> Returns the package name of the latest sucessful import

Example:
load "weblib.ring"
import System.web
see packagename() # system.web

42.29. packagename() Function 283


CHAPTER

FORTYTHREE

STDLIB FUNCTIONS

In this chapter we are going to learn about functions in the stdlib.ring


Before using the functions in the library, We must load the library first
load "stdlib.ring"

43.1 puts() function

print the value then print new line (nl)


Syntax:
puts(expr)

Example:
Load "stdlib.ring"

Puts("Hello, World!")

43.2 print() function

print string - support \n,\t and \r


Also we can use #{variable_name} to insert variables values.
Syntax:
print(string) ---> String

Example:
print("\nHello, World\n\nHow are you? \t\t I'm fine!\n")
x=10 y=20
print("\nx value = #{x} , y value = #{y} \n")

43.3 Print2Str() Function

Syntax:

284

You might also like