facets 2.8.2 → 2.8.3
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +13 -11
- data/HISTORY.rdoc +58 -0
- data/lib/core/facets/array/recursive.rb +91 -0
- data/lib/core/facets/array/recursively.rb +2 -2
- data/lib/core/facets/array/traverse.rb +23 -6
- data/lib/core/facets/enumerable/collisions.rb +1 -0
- data/lib/core/facets/enumerable/commonality.rb +4 -2
- data/lib/core/facets/enumerable/graph.rb +37 -1
- data/lib/core/facets/enumerable/mash.rb +1 -39
- data/lib/core/facets/enumerable/recursive.rb +75 -0
- data/lib/core/facets/enumerable/visit.rb +30 -0
- data/lib/core/facets/file/ext.rb +36 -0
- data/lib/core/facets/hash/graph.rb +18 -0
- data/lib/core/facets/hash/mash.rb +1 -18
- data/lib/core/facets/hash/recursive.rb +180 -0
- data/lib/core/facets/hash/recursive_merge.rb +6 -0
- data/lib/core/facets/hash/recursively.rb +2 -2
- data/lib/core/facets/hash/to_module.rb +26 -0
- data/lib/core/facets/hash/to_proc.rb +2 -2
- data/lib/core/facets/hash/traverse.rb +19 -13
- data/lib/core/facets/kernel/assign.rb +63 -0
- data/lib/core/facets/kernel/assign_from.rb +45 -0
- data/lib/core/facets/kernel/dup.rb +63 -0
- data/lib/core/facets/kernel/instance.rb +156 -0
- data/lib/core/facets/kernel/instance_assign.rb +1 -22
- data/lib/core/facets/kernel/meta_def.rb +4 -0
- data/lib/core/facets/kernel/populate.rb +1 -74
- data/lib/core/facets/kernel/set_from.rb +2 -0
- data/lib/core/facets/kernel/try_dup.rb +1 -0
- data/lib/core/facets/module/set.rb +36 -0
- data/lib/core/facets/objectspace/reflect.rb +45 -0
- data/lib/core/facets/struct/attributes.rb +6 -2
- data/lib/core/facets/symbol/op_div.rb +19 -0
- data/lib/core/facets/to_hash.rb +12 -0
- data/lib/more/facets/casting_hash.rb +172 -0
- data/lib/more/facets/pathname.rb +36 -0
- data/lib/more/facets/prepend.rb +57 -0
- data/lib/more/facets/random.rb +19 -3
- data/lib/more/facets/roman.rb +46 -153
- data/lib/more/facets/stash.rb +148 -33
- data/meta/released +1 -1
- data/meta/version +1 -1
- data/test/core/array/test_recursive.rb +18 -0
- data/test/core/enumerable/test_recursive.rb +18 -0
- data/test/core/file/test_ext.rb +31 -0
- data/test/core/hash/test_recursive.rb +23 -0
- data/test/core/hash/test_to_module.rb +21 -0
- data/test/core/kernel/test_assign.rb +57 -0
- data/test/core/kernel/test_assign_from.rb +20 -0
- data/test/more/test_prepend.rb +28 -0
- data/test/more/test_random.rb +40 -4
- metadata +39 -10
- data/lib/core/facets/kernel/instance_variables.rb +0 -97
- data/lib/more/facets/instance_eval.rb +0 -50
- data/lib/more/facets/ioredirect.rb +0 -77
- data/lib/more/facets/plugin_manager.rb +0 -50
- data/test/core/kernel/test_populate.rb +0 -46
data/test/more/test_random.rb
CHANGED
@@ -13,10 +13,46 @@ end
|
|
13
13
|
|
14
14
|
class TestRangeRandom < Test::Unit::TestCase
|
15
15
|
|
16
|
-
def
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
def assert_random_range(range, *msg)
|
17
|
+
assert_nothing_raised{ range.at_rand }
|
18
|
+
assert(range.include?( range.at_rand ), *msg)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_at_rand_normal
|
22
|
+
20.times do
|
23
|
+
assert_random_range(1..4)
|
24
|
+
assert_random_range(1...4)
|
25
|
+
assert_random_range(1.5..2.5)
|
26
|
+
assert_random_range(1.5...2.5)
|
27
|
+
assert_random_range(-4..-1)
|
28
|
+
assert_random_range(-4...-1)
|
29
|
+
assert_random_range(-2.5..-1.5)
|
30
|
+
assert_random_range(-2.5...-1.5)
|
31
|
+
assert_random_range('a'..'d')
|
32
|
+
assert_random_range('a'...'d')
|
33
|
+
assert_equal(5, (5..5).at_rand)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_at_rand_reverse
|
38
|
+
20.times do
|
39
|
+
assert_equal(nil,(4..1).at_rand)
|
40
|
+
assert_equal(nil,(4...1).at_rand)
|
41
|
+
assert_equal(nil,(-1..-4).at_rand)
|
42
|
+
assert_equal(nil,(-1...-4).at_rand)
|
43
|
+
#assert((1..4).include?((4..1).at_rand))
|
44
|
+
#assert((-4...-1).include?((-1...-4).at_rand))
|
45
|
+
#assert((-4..-1).include?((-1..-4).at_rand))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# I'd rather it return the first sentinel than +nil+.
|
50
|
+
# Really, ruby should be raising an error as an invalid range value.
|
51
|
+
def test_at_rand_exclusive
|
52
|
+
assert_equal(nil,(5...5).at_rand)
|
53
|
+
#assert_raises(RangeError) do
|
54
|
+
# (5...5).at_rand
|
55
|
+
#end
|
20
56
|
end
|
21
57
|
|
22
58
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 2
|
7
7
|
- 8
|
8
|
-
-
|
9
|
-
version: 2.8.
|
8
|
+
- 3
|
9
|
+
version: 2.8.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Thomas Sawyer <transfire@gmail.com>
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-04-10 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -55,6 +55,7 @@ files:
|
|
55
55
|
- lib/core/facets/array/pad.rb
|
56
56
|
- lib/core/facets/array/permutation.rb
|
57
57
|
- lib/core/facets/array/product.rb
|
58
|
+
- lib/core/facets/array/recursive.rb
|
58
59
|
- lib/core/facets/array/recursively.rb
|
59
60
|
- lib/core/facets/array/rotate.rb
|
60
61
|
- lib/core/facets/array/select.rb
|
@@ -107,6 +108,7 @@ files:
|
|
107
108
|
- lib/core/facets/enumerable/cluster_by.rb
|
108
109
|
- lib/core/facets/enumerable/collapse.rb
|
109
110
|
- lib/core/facets/enumerable/collect_with_index.rb
|
111
|
+
- lib/core/facets/enumerable/collisions.rb
|
110
112
|
- lib/core/facets/enumerable/commonality.rb
|
111
113
|
- lib/core/facets/enumerable/compact_map.rb
|
112
114
|
- lib/core/facets/enumerable/count.rb
|
@@ -136,10 +138,12 @@ files:
|
|
136
138
|
- lib/core/facets/enumerable/per.rb
|
137
139
|
- lib/core/facets/enumerable/probability.rb
|
138
140
|
- lib/core/facets/enumerable/purge.rb
|
141
|
+
- lib/core/facets/enumerable/recursive.rb
|
139
142
|
- lib/core/facets/enumerable/split.rb
|
140
143
|
- lib/core/facets/enumerable/sum.rb
|
141
144
|
- lib/core/facets/enumerable/take.rb
|
142
145
|
- lib/core/facets/enumerable/uniq_by.rb
|
146
|
+
- lib/core/facets/enumerable/visit.rb
|
143
147
|
- lib/core/facets/enumerable.rb
|
144
148
|
- lib/core/facets/enumerator/fx.rb
|
145
149
|
- lib/core/facets/enumerator.rb
|
@@ -149,6 +153,7 @@ files:
|
|
149
153
|
- lib/core/facets/exception.rb
|
150
154
|
- lib/core/facets/file/append.rb
|
151
155
|
- lib/core/facets/file/create.rb
|
156
|
+
- lib/core/facets/file/ext.rb
|
152
157
|
- lib/core/facets/file/null.rb
|
153
158
|
- lib/core/facets/file/read.rb
|
154
159
|
- lib/core/facets/file/rewrite.rb
|
@@ -174,6 +179,7 @@ files:
|
|
174
179
|
- lib/core/facets/hash/delete.rb
|
175
180
|
- lib/core/facets/hash/diff.rb
|
176
181
|
- lib/core/facets/hash/except.rb
|
182
|
+
- lib/core/facets/hash/graph.rb
|
177
183
|
- lib/core/facets/hash/group_by_value.rb
|
178
184
|
- lib/core/facets/hash/insert.rb
|
179
185
|
- lib/core/facets/hash/inverse.rb
|
@@ -189,6 +195,7 @@ files:
|
|
189
195
|
- lib/core/facets/hash/op_or.rb
|
190
196
|
- lib/core/facets/hash/op_push.rb
|
191
197
|
- lib/core/facets/hash/op_sub.rb
|
198
|
+
- lib/core/facets/hash/recursive.rb
|
192
199
|
- lib/core/facets/hash/recursive_merge.rb
|
193
200
|
- lib/core/facets/hash/recursively.rb
|
194
201
|
- lib/core/facets/hash/rekey.rb
|
@@ -199,6 +206,7 @@ files:
|
|
199
206
|
- lib/core/facets/hash/swap.rb
|
200
207
|
- lib/core/facets/hash/symbolize_keys.rb
|
201
208
|
- lib/core/facets/hash/to_h.rb
|
209
|
+
- lib/core/facets/hash/to_module.rb
|
202
210
|
- lib/core/facets/hash/to_options.rb
|
203
211
|
- lib/core/facets/hash/to_proc.rb
|
204
212
|
- lib/core/facets/hash/to_struct.rb
|
@@ -227,6 +235,8 @@ files:
|
|
227
235
|
- lib/core/facets/kernel/__set__.rb
|
228
236
|
- lib/core/facets/kernel/as.rb
|
229
237
|
- lib/core/facets/kernel/ask.rb
|
238
|
+
- lib/core/facets/kernel/assign.rb
|
239
|
+
- lib/core/facets/kernel/assign_from.rb
|
230
240
|
- lib/core/facets/kernel/attr_singleton.rb
|
231
241
|
- lib/core/facets/kernel/callstack.rb
|
232
242
|
- lib/core/facets/kernel/complete.rb
|
@@ -234,6 +244,7 @@ files:
|
|
234
244
|
- lib/core/facets/kernel/d.rb
|
235
245
|
- lib/core/facets/kernel/deep_copy.rb
|
236
246
|
- lib/core/facets/kernel/demo.rb
|
247
|
+
- lib/core/facets/kernel/dup.rb
|
237
248
|
- lib/core/facets/kernel/eigenclass.rb
|
238
249
|
- lib/core/facets/kernel/equate.rb
|
239
250
|
- lib/core/facets/kernel/ergo.rb
|
@@ -243,11 +254,11 @@ files:
|
|
243
254
|
- lib/core/facets/kernel/here.rb
|
244
255
|
- lib/core/facets/kernel/identical.rb
|
245
256
|
- lib/core/facets/kernel/in.rb
|
257
|
+
- lib/core/facets/kernel/instance.rb
|
246
258
|
- lib/core/facets/kernel/instance_assign.rb
|
247
259
|
- lib/core/facets/kernel/instance_class.rb
|
248
260
|
- lib/core/facets/kernel/instance_exec.rb
|
249
261
|
- lib/core/facets/kernel/instance_send.rb
|
250
|
-
- lib/core/facets/kernel/instance_variables.rb
|
251
262
|
- lib/core/facets/kernel/maybe.rb
|
252
263
|
- lib/core/facets/kernel/meta_alias.rb
|
253
264
|
- lib/core/facets/kernel/meta_class.rb
|
@@ -268,12 +279,14 @@ files:
|
|
268
279
|
- lib/core/facets/kernel/resc.rb
|
269
280
|
- lib/core/facets/kernel/respond.rb
|
270
281
|
- lib/core/facets/kernel/returning.rb
|
282
|
+
- lib/core/facets/kernel/set_from.rb
|
271
283
|
- lib/core/facets/kernel/silence.rb
|
272
284
|
- lib/core/facets/kernel/singleton_class.rb
|
273
285
|
- lib/core/facets/kernel/source_location.rb
|
274
286
|
- lib/core/facets/kernel/tap.rb
|
275
287
|
- lib/core/facets/kernel/true.rb
|
276
288
|
- lib/core/facets/kernel/try.rb
|
289
|
+
- lib/core/facets/kernel/try_dup.rb
|
277
290
|
- lib/core/facets/kernel/val.rb
|
278
291
|
- lib/core/facets/kernel/with.rb
|
279
292
|
- lib/core/facets/kernel.rb
|
@@ -312,6 +325,7 @@ files:
|
|
312
325
|
- lib/core/facets/module/redirect_method.rb
|
313
326
|
- lib/core/facets/module/rename_method.rb
|
314
327
|
- lib/core/facets/module/revise.rb
|
328
|
+
- lib/core/facets/module/set.rb
|
315
329
|
- lib/core/facets/module/spacename.rb
|
316
330
|
- lib/core/facets/module/wrap_method.rb
|
317
331
|
- lib/core/facets/module.rb
|
@@ -326,6 +340,7 @@ files:
|
|
326
340
|
- lib/core/facets/numeric.rb
|
327
341
|
- lib/core/facets/objectspace/classes.rb
|
328
342
|
- lib/core/facets/objectspace/op_fetch.rb
|
343
|
+
- lib/core/facets/objectspace/reflect.rb
|
329
344
|
- lib/core/facets/objectspace.rb
|
330
345
|
- lib/core/facets/proc/bind.rb
|
331
346
|
- lib/core/facets/proc/bind_to.rb
|
@@ -408,6 +423,7 @@ files:
|
|
408
423
|
- lib/core/facets/symbol/chomp.rb
|
409
424
|
- lib/core/facets/symbol/generate.rb
|
410
425
|
- lib/core/facets/symbol/not.rb
|
426
|
+
- lib/core/facets/symbol/op_div.rb
|
411
427
|
- lib/core/facets/symbol/plain.rb
|
412
428
|
- lib/core/facets/symbol/query.rb
|
413
429
|
- lib/core/facets/symbol/re_s.rb
|
@@ -443,6 +459,7 @@ files:
|
|
443
459
|
- lib/more/facets/autoreload.rb
|
444
460
|
- lib/more/facets/basicobject.rb
|
445
461
|
- lib/more/facets/blankslate.rb
|
462
|
+
- lib/more/facets/casting_hash.rb
|
446
463
|
- lib/more/facets/cgi.rb
|
447
464
|
- lib/more/facets/class_extend.rb
|
448
465
|
- lib/more/facets/cloneable.rb
|
@@ -470,10 +487,8 @@ files:
|
|
470
487
|
- lib/more/facets/hook.rb
|
471
488
|
- lib/more/facets/inheritor.rb
|
472
489
|
- lib/more/facets/ini.rb
|
473
|
-
- lib/more/facets/instance_eval.rb
|
474
490
|
- lib/more/facets/instance_function.rb
|
475
491
|
- lib/more/facets/instantiable.rb
|
476
|
-
- lib/more/facets/ioredirect.rb
|
477
492
|
- lib/more/facets/linkedlist.rb
|
478
493
|
- lib/more/facets/main.rb
|
479
494
|
- lib/more/facets/matcher.rb
|
@@ -502,8 +517,8 @@ files:
|
|
502
517
|
- lib/more/facets/pathlist.rb
|
503
518
|
- lib/more/facets/pathname.rb
|
504
519
|
- lib/more/facets/platform.rb
|
505
|
-
- lib/more/facets/plugin_manager.rb
|
506
520
|
- lib/more/facets/preinitialize.rb
|
521
|
+
- lib/more/facets/prepend.rb
|
507
522
|
- lib/more/facets/random.rb
|
508
523
|
- lib/more/facets/rbconfig.rb
|
509
524
|
- lib/more/facets/rbsystem.rb
|
@@ -549,6 +564,7 @@ files:
|
|
549
564
|
- test/core/array/test_pad.rb
|
550
565
|
- test/core/array/test_permutation.rb
|
551
566
|
- test/core/array/test_product.rb
|
567
|
+
- test/core/array/test_recursive.rb
|
552
568
|
- test/core/array/test_rotate.rb
|
553
569
|
- test/core/array/test_select.rb
|
554
570
|
- test/core/array/test_stackable.rb
|
@@ -594,11 +610,13 @@ files:
|
|
594
610
|
- test/core/enumerable/test_one.rb
|
595
611
|
- test/core/enumerable/test_per.rb
|
596
612
|
- test/core/enumerable/test_probability.rb
|
613
|
+
- test/core/enumerable/test_recursive.rb
|
597
614
|
- test/core/enumerable/test_split.rb
|
598
615
|
- test/core/enumerable/test_take.rb
|
599
616
|
- test/core/enumerable/test_uniq_by.rb
|
600
617
|
- test/core/exception/test_exception.rb
|
601
618
|
- test/core/file/test_create.rb
|
619
|
+
- test/core/file/test_ext.rb
|
602
620
|
- test/core/file/test_file.rb
|
603
621
|
- test/core/file/test_null.rb
|
604
622
|
- test/core/file/test_rewrite.rb
|
@@ -624,6 +642,7 @@ files:
|
|
624
642
|
- test/core/hash/test_op_or.rb
|
625
643
|
- test/core/hash/test_op_push.rb
|
626
644
|
- test/core/hash/test_op_sub.rb
|
645
|
+
- test/core/hash/test_recursive.rb
|
627
646
|
- test/core/hash/test_rekey.rb
|
628
647
|
- test/core/hash/test_replace_each.rb
|
629
648
|
- test/core/hash/test_select.rb
|
@@ -631,6 +650,7 @@ files:
|
|
631
650
|
- test/core/hash/test_stringify_keys.rb
|
632
651
|
- test/core/hash/test_swap.rb
|
633
652
|
- test/core/hash/test_symbolize_keys.rb
|
653
|
+
- test/core/hash/test_to_module.rb
|
634
654
|
- test/core/hash/test_traverse.rb
|
635
655
|
- test/core/hash/test_update_each.rb
|
636
656
|
- test/core/hash/test_update_keys.rb
|
@@ -646,6 +666,8 @@ files:
|
|
646
666
|
- test/core/integer/test_of.rb
|
647
667
|
- test/core/integer/test_ordinal.rb
|
648
668
|
- test/core/kernel/test_as.rb
|
669
|
+
- test/core/kernel/test_assign.rb
|
670
|
+
- test/core/kernel/test_assign_from.rb
|
649
671
|
- test/core/kernel/test_attr_singleton.rb
|
650
672
|
- test/core/kernel/test_callstack.rb
|
651
673
|
- test/core/kernel/test_constant.rb
|
@@ -658,7 +680,6 @@ files:
|
|
658
680
|
- test/core/kernel/test_not_nil.rb
|
659
681
|
- test/core/kernel/test_object_class.rb
|
660
682
|
- test/core/kernel/test_object_hexid.rb
|
661
|
-
- test/core/kernel/test_populate.rb
|
662
683
|
- test/core/kernel/test_resc.rb
|
663
684
|
- test/core/kernel/test_returning.rb
|
664
685
|
- test/core/kernel/test_silence.rb
|
@@ -794,6 +815,7 @@ files:
|
|
794
815
|
- test/more/test_openobject.rb
|
795
816
|
- test/more/test_ostruct.rb
|
796
817
|
- test/more/test_partial.rb
|
818
|
+
- test/more/test_prepend.rb
|
797
819
|
- test/more/test_random.rb
|
798
820
|
- test/more/test_shellwords.rb
|
799
821
|
- test/more/test_succ.rb
|
@@ -835,7 +857,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
835
857
|
requirements: []
|
836
858
|
|
837
859
|
rubyforge_project: facets
|
838
|
-
rubygems_version: 1.3.6
|
860
|
+
rubygems_version: 1.3.6
|
839
861
|
signing_key:
|
840
862
|
specification_version: 3
|
841
863
|
summary: Premium Core Extensions and Standard Additions
|
@@ -851,6 +873,7 @@ test_files:
|
|
851
873
|
- test/core/array/test_pad.rb
|
852
874
|
- test/core/array/test_permutation.rb
|
853
875
|
- test/core/array/test_product.rb
|
876
|
+
- test/core/array/test_recursive.rb
|
854
877
|
- test/core/array/test_rotate.rb
|
855
878
|
- test/core/array/test_select.rb
|
856
879
|
- test/core/array/test_stackable.rb
|
@@ -896,11 +919,13 @@ test_files:
|
|
896
919
|
- test/core/enumerable/test_one.rb
|
897
920
|
- test/core/enumerable/test_per.rb
|
898
921
|
- test/core/enumerable/test_probability.rb
|
922
|
+
- test/core/enumerable/test_recursive.rb
|
899
923
|
- test/core/enumerable/test_split.rb
|
900
924
|
- test/core/enumerable/test_take.rb
|
901
925
|
- test/core/enumerable/test_uniq_by.rb
|
902
926
|
- test/core/exception/test_exception.rb
|
903
927
|
- test/core/file/test_create.rb
|
928
|
+
- test/core/file/test_ext.rb
|
904
929
|
- test/core/file/test_file.rb
|
905
930
|
- test/core/file/test_null.rb
|
906
931
|
- test/core/file/test_rewrite.rb
|
@@ -926,6 +951,7 @@ test_files:
|
|
926
951
|
- test/core/hash/test_op_or.rb
|
927
952
|
- test/core/hash/test_op_push.rb
|
928
953
|
- test/core/hash/test_op_sub.rb
|
954
|
+
- test/core/hash/test_recursive.rb
|
929
955
|
- test/core/hash/test_rekey.rb
|
930
956
|
- test/core/hash/test_replace_each.rb
|
931
957
|
- test/core/hash/test_select.rb
|
@@ -933,6 +959,7 @@ test_files:
|
|
933
959
|
- test/core/hash/test_stringify_keys.rb
|
934
960
|
- test/core/hash/test_swap.rb
|
935
961
|
- test/core/hash/test_symbolize_keys.rb
|
962
|
+
- test/core/hash/test_to_module.rb
|
936
963
|
- test/core/hash/test_traverse.rb
|
937
964
|
- test/core/hash/test_update_each.rb
|
938
965
|
- test/core/hash/test_update_keys.rb
|
@@ -948,6 +975,8 @@ test_files:
|
|
948
975
|
- test/core/integer/test_of.rb
|
949
976
|
- test/core/integer/test_ordinal.rb
|
950
977
|
- test/core/kernel/test_as.rb
|
978
|
+
- test/core/kernel/test_assign.rb
|
979
|
+
- test/core/kernel/test_assign_from.rb
|
951
980
|
- test/core/kernel/test_attr_singleton.rb
|
952
981
|
- test/core/kernel/test_callstack.rb
|
953
982
|
- test/core/kernel/test_constant.rb
|
@@ -960,7 +989,6 @@ test_files:
|
|
960
989
|
- test/core/kernel/test_not_nil.rb
|
961
990
|
- test/core/kernel/test_object_class.rb
|
962
991
|
- test/core/kernel/test_object_hexid.rb
|
963
|
-
- test/core/kernel/test_populate.rb
|
964
992
|
- test/core/kernel/test_resc.rb
|
965
993
|
- test/core/kernel/test_returning.rb
|
966
994
|
- test/core/kernel/test_silence.rb
|
@@ -1096,6 +1124,7 @@ test_files:
|
|
1096
1124
|
- test/more/test_openobject.rb
|
1097
1125
|
- test/more/test_ostruct.rb
|
1098
1126
|
- test/more/test_partial.rb
|
1127
|
+
- test/more/test_prepend.rb
|
1099
1128
|
- test/more/test_random.rb
|
1100
1129
|
- test/more/test_shellwords.rb
|
1101
1130
|
- test/more/test_succ.rb
|
@@ -1,97 +0,0 @@
|
|
1
|
-
module Kernel
|
2
|
-
|
3
|
-
def instance_vars
|
4
|
-
InstanceVariables.new(self)
|
5
|
-
end
|
6
|
-
|
7
|
-
end
|
8
|
-
|
9
|
-
|
10
|
-
class InstanceVariables
|
11
|
-
|
12
|
-
include Enumerable
|
13
|
-
|
14
|
-
def initialize(delegate)
|
15
|
-
@delegate = delegate
|
16
|
-
end
|
17
|
-
|
18
|
-
def instance_delegate
|
19
|
-
@delegate
|
20
|
-
end
|
21
|
-
|
22
|
-
def each
|
23
|
-
@delegate.instance_variables.each do |name|
|
24
|
-
yield(name[1..-1].to_sym, @delegate.instance_variable_get(name))
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def to_hash
|
29
|
-
h = {}
|
30
|
-
each do |name, value|
|
31
|
-
h[name] = value
|
32
|
-
end
|
33
|
-
h
|
34
|
-
end
|
35
|
-
|
36
|
-
def [](name)
|
37
|
-
name = atize(name)
|
38
|
-
@delegate.instance_variable_get(name)
|
39
|
-
end
|
40
|
-
|
41
|
-
def []=(name, value)
|
42
|
-
name = atize(name)
|
43
|
-
@delegate.instance_variable_set(name,value)
|
44
|
-
end
|
45
|
-
|
46
|
-
def <<(pair)
|
47
|
-
name, value = *pair
|
48
|
-
name = atize(name)
|
49
|
-
@delegate.instance_variable_set(name, value)
|
50
|
-
end
|
51
|
-
|
52
|
-
# (See also: Kernel#populate, which uses accessor method rather than setting instance variables directly.)
|
53
|
-
def update(hash)
|
54
|
-
hash.each do |pair|
|
55
|
-
self << pair
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
def keys
|
60
|
-
@delegate.instance_variables.collect do |name|
|
61
|
-
name[1..-1].to_sym
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
def names
|
66
|
-
@delegate.instance_variables.collect do |name|
|
67
|
-
name[1..-1]
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
def values
|
72
|
-
@delegate.instance_variables.collect do |name|
|
73
|
-
@delegate.instance_variable_get(name)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
private
|
78
|
-
|
79
|
-
def atize(name)
|
80
|
-
name !~ /^@/ ? "@#{name}" : name
|
81
|
-
end
|
82
|
-
|
83
|
-
end
|
84
|
-
|
85
|
-
=begin demo
|
86
|
-
class Friend
|
87
|
-
attr_accessor :name, :age, :phone
|
88
|
-
def initialize(name, age, phone)
|
89
|
-
@name, @age, @phone = name, age, phone
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
f1 = Friend.new("John", 30, "555-1212")
|
94
|
-
p f1.instance_vars
|
95
|
-
f1.instance_vars.update({:name=>'Jerry'})
|
96
|
-
p f1.instance_vars
|
97
|
-
=end
|
@@ -1,50 +0,0 @@
|
|
1
|
-
require 'facets/functor'
|
2
|
-
|
3
|
-
class Object
|
4
|
-
|
5
|
-
# Access private internals of an object with a fluid notation.
|
6
|
-
#
|
7
|
-
# class X
|
8
|
-
# attr :a
|
9
|
-
# private :a
|
10
|
-
# def initialize
|
11
|
-
# @a = 1
|
12
|
-
# end
|
13
|
-
# end
|
14
|
-
#
|
15
|
-
# x = X.new
|
16
|
-
# p x.instance_eval.a #=> 1
|
17
|
-
# p x.a #=> Error
|
18
|
-
#
|
19
|
-
# A useful example might include adding accessors to a metaclass.
|
20
|
-
#
|
21
|
-
# class X
|
22
|
-
# metaclass.instance_eval.attr :x
|
23
|
-
# end
|
24
|
-
#
|
25
|
-
# This method is called #instance to go along with methods
|
26
|
-
# that provide similar access, such as #instance_variables
|
27
|
-
# and #instance_eval. In fact, one could argue this would be
|
28
|
-
# a good return value of #instance_eval is no block is given.
|
29
|
-
#
|
30
|
-
# This method was once called #pry and #privy.
|
31
|
-
#
|
32
|
-
# TODO: Will only support calls with blocks as of Ruby 1.9+.
|
33
|
-
#
|
34
|
-
def instance_eval(*args, &block)
|
35
|
-
return super if block or !args.empty?
|
36
|
-
@_instance_eval ||= Functor.new do |op, *a|
|
37
|
-
instance_eval{ send(op, *a) }
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
# TODO for Ruby 1.9
|
42
|
-
#def instance_eval(*args, &block)
|
43
|
-
# return super if block or !args.empty?
|
44
|
-
# @_instance_functor ||= Functor.new do |op, *a, &b|
|
45
|
-
# fcall(op, *a, &b)
|
46
|
-
# end
|
47
|
-
#end
|
48
|
-
|
49
|
-
end
|
50
|
-
|