-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path09.rb
401 lines (312 loc) · 6.22 KB
/
09.rb
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
class Point
def initialize(x,y)
@x,@y = x,y
end
private_class_method :new
def Point.cartesian(x,y)
new(x,y)
end
def Point.polar(r, theta)
new(r*Math.cos(theta), r*Math.sin(theta))
end
end
class Point2 < Point; end
p Point2.polar(2, 1)
n = Point2.new(2,1) rescue 'ops'
p n
p 'po'
puts '-------------'
class PointDup
UMACONSTANTE = ''
@@sopraver = ''
def initialize(*coords)
@coords = coords
end
def initialize_copy(orig)
@coords = @coords.dup
end
end
puts '-------------'
class Season
NAMES = %w{ Spring Summer Autumn Winter }
INSTANCES = []
def initialize(n)
@n = n
end
def to_s
NAMES[@n]
end
NAMES.each_with_index do |name,index|
instance = new(index)
INSTANCES[index] = instance
const_set name, instance
# Define a constant to refer to it
end
def _dump(limit)
@n.to_s
end
def self._load(s)
INSTANCES[Integer(s)]
end
private_class_method :new,:allocate
private :dup, :clone
end
puts '-------------'
require 'singleton'
class PointStat
include Singleton
def initialize
@n, @totalX, @totalY = 0, 0.0, 0.0
end
def record(point)
@n += 1
@totalX += point.x
@totalY += point.y
end
def report
puts "Number of points created: #@n"
puts "Average X coordinate: #{@totalX/@n}"
puts "Average Y coordinate: #{@totalY/@n}"
end
class << self
# class methods go here as instance methods of the eigenclass
end
end
#def initialize(x,y)
# @x,@y = x,y
# PointStats.instance.record(self)
#end
puts '-------------'
# If your class defines <=>, you can mix
# in Comparable to get <, <=, == >, >=,
# and between? for free.
puts '-------------'
countdown = Object.new
def countdown.each
yield 3
yield 2
yield 1
end
countdown.extend(Enumerable)
print countdown.sort, "\n"
puts '-------------'
puts Math.sin(0)
include Math
puts sin(0)
puts '-------------'
puts $LOAD_PATH
puts '-------------'
class << PointStat
def class_method1
puts 'oi'
end
def class_method2
end
end
PointStat.class_method1
puts '-------------'
def Integer.parse(text)
text.to_i
end
p Fixnum.parse("1")
p PointStat.inspect
p String.ancestors
p Fixnum.ancestors
class Object
def const_missing name
'fuck'
end
end
class << Object
def const_missing name
'fuck instance'
end
end
class Opa
def oi
p S
end
p S
end
Opa.new.oi
puts '-------------'
p String.ancestors
p String.included_modules
module StringTest
def test
p 'test'
end
end
String.extend StringTest
String.test
puts '-------------'
module M2
class C2
p Module.nesting
end
end
puts '-------------'
M = Module.new
C = Class.new
D = Class.new(C) {
include M
}
p D.to_s
puts '-------------'
x = 1
p eval "x + 1"
puts '-------------'
class Object
def bindings
binding
end
end
class Test
def initialize(x); @x = x; end
end
t = Test.new(10)
p (eval '@x', t.bindings)
p t.instance_eval("@x")
String.class_eval {
alias len size
}
p "123".len
String.instance_eval{
def empty
''
end
}
p String.empty
puts '-------------'
p global_variables
puts '-------------'
p local_variables
puts '-------------'
p PointDup.new.instance_variables
p PointDup.class_variables
p PointDup.constants
puts '-------------'
o = Object.new
o.instance_variable_set :@x, 0
o.instance_variable_get :@x
p o.instance_variable_defined? :@x
p Season.constants
p Season::Summer
puts '-------------'
Math.const_set :EPI, Math::E*Math::PI
p Math.const_get :EPI
p Math.const_defined? :EPI
# o.instance_eval { remove_instance_variable :@x }
# String.class_eval { remove_class_variable(:@@x) }
Math.send :remove_const, :EPI
p Math.const_defined? :EPI
puts '-------------'
def Symbol.const_missing(name)
name
end
p Symbol::FuckRehab
puts '-------------'
p "".methods
puts '-------------'
p "".public_methods false #Exclude inherited methods
puts '-------------'
p PointStat.singleton_methods
puts '-------------'
p String.singleton_methods
puts '-------------'
p String.instance_methods == "s".public_methods
p String.instance_methods(false) == "s".public_methods(false)
p String.public_instance_methods == String.instance_methods
p String.private_instance_methods false
puts '-------------'
p Math.singleton_methods
puts '-------------'
p String.public_method_defined? :reverse
p String.protected_method_defined? :reverse
p String.private_method_defined? :initialize
p String.method_defined? :upcase!
puts '-------------'
p "s".method :reverse # => Method object
p String.instance_method :reverse # => UnboundMethod object
"hello".send :puts, "world"
puts '-------------'
def add_method(c, m, &b)
c.class_eval {
define_method m, &b
}
end
add_method(String, :greet) { "Hello, " + self }
p "world".greet
def add_class_method(c, m, &b)
eigenclass = class << c; self; end
eigenclass.class_eval {
define_method(m, &b)
}
end
add_class_method(String, :greet) {|name| "Hello, " + name }
p String.greet "world"
puts '-------------'
def backup(c, m, prefix="original")
n = :"#{prefix}_#{m}"
c.class_eval {
alias_method n, m
}
end
backup String, :reverse
p "test".original_reverse
puts '-------------'
class Hash
def method_missing(key, *args)
text = key.to_s
if text[-1,1] == "="
self[text.chop.to_sym] = args[0]
else
self[key]
end
end
end
h = {}
h.one = 1
puts h.one
puts '-------------'
def Object.inherited c
puts "class #{c} < #{self}"
end
module Final
def self.included(c)
c.instance_eval do
def inherited(sub)
raise Exception, "Attempt to create subclass #{sub} of Final class #{self}"
end
end
end
end
class ToVendo; include Final; end
# class NaoPode < ToVendo; end
puts '-------------'
def String.method_added(name)
puts "New instance method #{name} added to String"
end
def String.singleton_method_added(name)
puts "New class method #{name} added to String"
end
module Strict
def singleton_method_added(name)
STDERR.puts "Warning: singleton #{name} added to a Strict object"
eigenclass = class << self; self; end
eigenclass.class_eval { remove_method name }
end
end
class String
def oi; end;
include Strict
end
class String
def oi2; end;
end
class << ""
def error; end
end
puts '-------------'
ObjectSpace.each_object(Class) {|c| puts c }
puts '-------------'