-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path06_objects.ring
More file actions
44 lines (35 loc) · 995 Bytes
/
06_objects.ring
File metadata and controls
44 lines (35 loc) · 995 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
06 - Python Objects
Create Python objects and call methods on them.
*/
load "python.ring"
py_init()
# Create a Python string object
pystr = py_object("hello world")
# Object info
? "Type: " + py_type(pystr)
? "Repr: " + py_repr(pystr)
? "Str: " + py_str(pystr)
? "Len: " + py_len(pystr)
# Call methods
? ""
? "upper(): " + py_call_method(pystr, "upper", [])
? "title(): " + py_call_method(pystr, "title", [])
? "replace('world'): " + py_call_method(pystr, "replace", ["world", "Ring"])
? "split(): "
words = py_call_method(pystr, "split", [])
for w in words
? " - " + w
next
# Type checking
? ""
pyint = py_object(42)
? "42 is int: " + py_isinstance(pyint, "int")
? "42 is str: " + py_isinstance(pyint, "str")
pyfloat = py_object(3.14)
? "3.14 type: " + py_type(pyfloat)
# Get value back into Ring
? ""
? "py_value(42): " + py_value(pyint)
? "py_value(3.14): " + py_value(pyfloat)
? "py_value('hello world'): " + py_value(pystr)