-
Notifications
You must be signed in to change notification settings - Fork 1
/
scope-test.reb
101 lines (71 loc) · 2.46 KB
/
scope-test.reb
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
Rebol [
Title: "Rubol Scope Test"
Purpose: "Demonstrate Rubol's simulated Ruby Scoping"
Description: {Rebol has a reputation for making all variables global.
This is not really the case, rather it lets you build your own scoping
in the interpreter. This shows Rebol following Ruby's rules, basically.}
Author: "Hostile Fork"
Home: http://hostilefork.com/
License: mit
File: %scope-test.reb
Date: 10-Nov-2009
Version: 0.1.1
]
do %rubol.reb
do %checkpoint.reb
checkpoint A [x y z]
y: "global assign Y"
z: "global assign Z"
checkpoint B [x y z]
class ScopeTest [
attr_accessor [.x .y]
def initialize (nil) [
.x: "member init .X"
.y: "member init .Y"
]
def set_XYZ [y] [
checkpoint E [.x .y .z]
.x: "set_XYZ assign .X"
.y: y
.z: "set_XYZ assign .Z"
; If you don't have some local scope, the existence of an
; accessor on a member creates an implicit ScopeTest/x
; It is protected and cannot be overwritten.
; this would cause an exception
; x: "set_XYZ assign X"
; Having a named parameter makes it okay to
; assign to it in this scope. The assignment changes the
; value of the parameter during this call, and leaves
; the accessor alone
y: "set_XYZ assign Y"
; If no accessor is defined then there is no z member and
; it is free to use as a local by default
z: "set_XYZ assign Z"
checkpoint F [.x .y .z]
]
]
checkpoint C [(test/x/get) (test/y/get) (test/z/get)]
checkpoint D [x y z]
test: ScopeTest/new []
test/set_XYZ ["param to set_XYZ[y]"]
checkpoint G [x y z]
checkpoint H [(test/x/get) (test/y/get) (test/z/get)]
unset 'y
unset 'z
unset 'test
checkpoint I [x y z]
checkpoint J [.x .y .z]
comment [{
This gives output consistent with the expectations of programmers familiar with
the scoping concepts of other languages.
A: [[x = unset!] [y = unset!] [z = unset!]]
B: [[x = unset!] [y = "global assign Y"] [z = "global assign Z"]]
C: [[(test/x/get) = *exception*] [(test/y/get) = *exception*] [(test/z/get) = *exception*]]
D: [[x = unset!] [y = "global assign Y"] [z = "global assign Z"]]
E: [[.x = "member init .X"] [.y = "member init .Y"] [.z = none]]
F: [[.x = "set_XYZ assign .X"] [.y = "param to set_XYZ[y]"] [.z = "set_XYZ assign .Z"]]
G: [[x = unset!] [y = "global assign Y"] [z = "global assign Z"]]
H: [[(test/x/get) = "set_XYZ assign .X"] [(test/y/get) = "param to set_XYZ[y]"] [(test/z/get) = *exception*]]
I: [[x = unset!] [y = unset!] [z = unset!]]
J: [[.x = unset!] [.y = unset!] [.z = unset!]]
}]