/
class.fy
198 lines (152 loc) · 4.74 KB
/
class.fy
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
class Class {
ruby_aliases: [ 'superclass, '===, 'ancestors, 'instance_methods, 'methods, 'inspect, 'to_s ]
def new {
"""
@return A new @Class@ subclassed from @Object@.
Creates a new @Class@ instance by subclassing @Object@.
"""
obj = allocate()
obj initialize
obj
}
# calls initialize:, if defined
def new: superclass {
"""
@superclass The superclass to inherit from.
@return A new @Class@ inherited from @superclass.
Creates a new @Class@ instance by subclassing @superclass.
"""
obj = allocate()
obj initialize: superclass
obj
}
def Class superclass: superclass body: body_block {
"""
@superclass The superclass to inherit from.
@body_block A @Block@ that is used as the body of the new @Class@.
@return A new @Class@ inherited from @superclass.
Creates a new @Class@ by subclassing @superclass and
using @body_block as its body.
"""
new(superclass, &body_block)
}
def initialize {
"""
Initializes a @Class@ with @Object@ set as superclass (default superclass).
"""
initialize: Object
}
def initialize: superclass {
"""
Initializes a @Class@ with a superclass.
"""
initialize(superclass)
}
def define_method: name with: block {
"""
@name Name of the method to be defined.
@block A @Block@ that is used as the method's body.
Defines an instance method on a @Class@ with a given name and
body.
"""
define_method(message_name: name, &block)
}
def undefine_method: name {
"""
@name Name of the method to undefine (remove) from a @Class@.
Undefines an instance method on a Class with a given name.
"""
remove_method(message_name: name)
}
def define_class_method: name with: block {
"""
@name Name of the class method to be defined.
@block A @Block@ to be used as the class methods body.
Defines a class method on a @Class@ (a singleton method) with a
given name and body.
"""
define_singleton_method: name with: block
}
def undefine_class_method: name {
"""
@name Name of the class method to undefine (remove).
Undefines a class method on a @Class@ with a given name.
"""
undefine_singleton_method: name
}
def subclass: body_block {
"""
@body_block A @Block@ that gets used as the body of the @Class@.
@return A new @Class@ inherited from @self.
Creates a new @Class@ with @self as superclass and the given body.
"""
Class superclass: self body: body_block
}
def nested_classes {
"""
@return @Array@ of all nested classes for @self.
Returns all the nested classes within a @Class@ as an @Array@.
"""
"Not Yet Implemented" raise!
}
def instance_method: name {
"""
@name Name of the instance method to return.
@return The instance @Method@ with the given @name or @nil.
Returns an instance method for a @Class@ with a given name.
"""
instance_method(message_name: name)
}
def alias_method_rbx: new_method_name for: old_method_name {
"""
Rbx specific version of alias_method:for: due to bootstrap order
reasons. Should not be used directly.
"""
alias_method(message_name: new_method_name, message_name: old_method_name)
}
def alias_method: new_method_name for_ruby: ruby_method_name {
"""
Creates a method alias for a Ruby method.
"""
alias_method(message_name: new_method_name, ruby_method_name)
}
def public: method_names {
"""
@method_names One or more (@Array@) method names (as a @Symbol@) to be set to public in this @Class@.
Sets any given method names to public on this @Class@.
"""
method_names = method_names to_a()
method_names = method_names map: |m| { message_name: m }
public(*method_names)
}
def private: method_names {
"""
@method_names One or more (@Array@) method names (as a @Symbol@) to be set to private in this @Class@.
Sets any given method names to private on this @Class@.
"""
method_names = method_names to_a()
method_names = method_names map() |m| { message_name: m }
private(*method_names)
}
def protected: method_names {
"""
@method_names One or more (@Array@) method names (as a @Symbol@) to be set to protected in this @Class@.
Sets any given method names to protected on this @Class@.
"""
method_names = method_names to_a()
method_names = method_names map() |m| { message_name: m }
protected(*method_names)
}
def forwards_unary_ruby_methods {
"""
Creates ruby_alias methods for any unary ruby methods of a class.
"""
instance_methods select() |m| {
m =~(/^[a-z]+/)
} select() |m| {
instance_method(m) arity() == 0
} each() |m| {
ruby_alias: m
}
}
}