-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path03_eval.ring
More file actions
38 lines (31 loc) · 715 Bytes
/
03_eval.ring
File metadata and controls
38 lines (31 loc) · 715 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
/*
03 - Expressions & Eval
Evaluate Python expressions and get typed results back in Ring.
*/
load "python.ring"
py_init()
# Numbers
? "2 ** 10 = " + py_eval("2 ** 10")
? "22 / 7 = " + py_eval("22 / 7")
# Strings
? py_eval("'hello' + ' ' + 'world'")
? py_eval("'ring-python'.upper()")
# Lists
squares = py_eval("[x**2 for x in range(6)]")
? "Squares: "
for s in squares
? " " + s
next
# Dicts (returned as list of [key, value] pairs)
? ""
person = py_eval("{'name': 'Alice', 'age': 30}")
? "Person:"
for pair in person
if islist(pair) and len(pair) = 2
? " " + pair[1] + ": " + pair[2]
ok
next
# Boolean
? ""
? "10 > 5 = " + py_eval("10 > 5")
? "10 < 5 = " + py_eval("10 < 5")