facets 2.1.3 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +14 -12
- data/CHANGES +45 -2
- data/NOTES +9 -7
- data/lib/core/facets/1stclassmethod.rb +15 -11
- data/lib/core/facets/array.rb +0 -1
- data/lib/core/facets/array/conjoin.rb +40 -8
- data/lib/core/facets/array/delete.rb +8 -2
- data/lib/core/facets/array/indexable.rb +30 -3
- data/lib/core/facets/array/merge.rb +11 -0
- data/lib/core/facets/array/only.rb +3 -3
- data/lib/core/facets/array/pad.rb +4 -46
- data/lib/core/facets/array/rotate.rb +4 -4
- data/lib/core/facets/array/select.rb +2 -0
- data/lib/core/facets/array/splice.rb +16 -2
- data/lib/core/facets/array/stackable.rb +6 -40
- data/lib/core/facets/binding.rb +3 -0
- data/lib/core/facets/binding/cflow.rb +10 -64
- data/lib/core/facets/binding/defined.rb +10 -0
- data/lib/core/facets/binding/eval.rb +10 -76
- data/lib/core/facets/binding/here.rb +9 -0
- data/lib/core/facets/binding/self.rb +10 -0
- data/lib/core/facets/binding/vars.rb +0 -45
- data/lib/core/facets/boolean.rb +31 -103
- data/lib/core/facets/class/descendents.rb +17 -44
- data/lib/core/facets/comparable/bound.rb +8 -70
- data/lib/core/facets/comparable/cmp.rb +12 -92
- data/lib/core/facets/conversion.rb +122 -321
- data/lib/core/facets/dir/multiglob.rb +0 -13
- data/lib/core/facets/dir/traverse.rb +61 -111
- data/lib/core/facets/enumerable.rb +4 -2
- data/lib/core/facets/enumerable/cartesian.rb +36 -85
- data/lib/core/facets/enumerable/collect.rb +64 -152
- data/lib/core/facets/enumerable/combination.rb +40 -42
- data/lib/core/facets/enumerable/count.rb +99 -174
- data/lib/core/facets/enumerable/each.rb +86 -0
- data/lib/core/facets/enumerable/mash.rb +58 -0
- data/lib/core/facets/enumerable/permutation.rb +39 -44
- data/lib/core/facets/enumerable/probability.rb +16 -90
- data/lib/core/facets/enumerable/split.rb +113 -0
- data/lib/core/facets/exception/detail.rb +1 -2
- data/lib/core/facets/facets.rb +3 -2
- data/lib/core/facets/file/read.rb +4 -59
- data/lib/core/facets/file/topath.rb +8 -96
- data/lib/core/facets/file/write.rb +21 -49
- data/lib/core/facets/filetest/root.rb +4 -3
- data/lib/core/facets/functor.rb +5 -43
- data/lib/core/facets/hash/at.rb +7 -37
- data/lib/core/facets/hash/delete.rb +9 -58
- data/lib/core/facets/hash/has_keys.rb +4 -43
- data/lib/core/facets/hash/insert.rb +5 -38
- data/lib/core/facets/hash/inverse.rb +2 -41
- data/lib/core/facets/hash/iterate.rb +4 -45
- data/lib/core/facets/hash/keyize.rb +24 -92
- data/lib/core/facets/hash/merge.rb +4 -38
- data/lib/core/facets/hash/new.rb +5 -38
- data/lib/core/facets/hash/op.rb +20 -66
- data/lib/core/facets/hash/rekey.rb +9 -87
- data/lib/core/facets/hash/select.rb +1 -1
- data/lib/core/facets/hash/slice.rb +42 -0
- data/lib/core/facets/hash/traverse.rb +6 -46
- data/lib/core/facets/hash/update.rb +27 -67
- data/lib/core/facets/hash/weave.rb +22 -42
- data/lib/core/facets/indexable.rb +42 -14
- data/lib/core/facets/integer/bitmask.rb +69 -106
- data/lib/core/facets/integer/factorial.rb +1 -44
- data/lib/core/facets/integer/multiples.rb +27 -22
- data/lib/core/facets/integer/of.rb +1 -33
- data/lib/core/facets/kernel.rb +4 -2
- data/lib/core/facets/kernel/ask.rb +15 -3
- data/lib/core/facets/kernel/callstack.rb +38 -69
- data/lib/core/facets/kernel/constant.rb +2 -38
- data/lib/core/facets/kernel/deepcopy.rb +3 -55
- data/lib/core/facets/kernel/dir.rb +2 -0
- data/lib/core/facets/kernel/ergo.rb +2 -2
- data/lib/core/facets/kernel/instance.rb +51 -120
- data/lib/core/facets/kernel/metaid.rb +35 -73
- data/lib/core/facets/kernel/object.rb +14 -39
- data/lib/core/facets/kernel/op_esc.rb +24 -2
- data/lib/core/facets/kernel/populate.rb +6 -69
- data/lib/core/facets/kernel/report.rb +28 -33
- data/lib/core/facets/kernel/require.rb +7 -21
- data/lib/core/facets/kernel/respond.rb +11 -1
- data/lib/core/facets/kernel/returning.rb +50 -0
- data/lib/core/facets/kernel/silence.rb +12 -24
- data/lib/core/facets/kernel/super.rb +12 -76
- data/lib/core/facets/kernel/tap.rb +25 -107
- data/lib/core/facets/kernel/val.rb +7 -46
- data/lib/core/facets/kernel/withattr.rb +6 -64
- data/lib/core/facets/matchdata/matchset.rb +8 -50
- data/lib/core/facets/module.rb +3 -1
- data/lib/core/facets/module/abstract.rb +9 -47
- data/lib/core/facets/module/alias.rb +11 -68
- data/lib/core/facets/module/attr.rb +36 -15
- data/lib/core/facets/module/cattr.rb +8 -76
- data/lib/core/facets/module/clone.rb +15 -66
- data/lib/core/facets/module/include.rb +54 -114
- data/lib/core/facets/module/methods.rb +15 -13
- data/lib/core/facets/module/modify.rb +20 -206
- data/lib/core/facets/module/name.rb +19 -83
- data/lib/core/facets/module/require.rb +48 -53
- data/lib/core/facets/module/traits.rb +74 -33
- data/lib/core/facets/nilclass/status.rb +25 -2
- data/lib/core/facets/numeric/round.rb +24 -89
- data/lib/core/facets/proc/bind.rb +16 -59
- data/lib/core/facets/proc/compose.rb +6 -40
- data/lib/core/facets/proc/fn.rb +2 -0
- data/lib/core/facets/range/combine.rb +15 -51
- data/lib/core/facets/range/overlap.rb +8 -64
- data/lib/core/facets/regexp/arity.rb +6 -41
- data/lib/core/facets/stackable.rb +43 -4
- data/lib/core/facets/string.rb +2 -0
- data/lib/core/facets/string/align.rb +45 -55
- data/lib/core/facets/string/blank.rb +2 -46
- data/lib/core/facets/string/bracket.rb +14 -87
- data/lib/core/facets/string/case.rb +45 -32
- data/lib/core/facets/string/crypt.rb +3 -40
- data/lib/core/facets/string/filter.rb +7 -49
- data/lib/core/facets/string/format.rb +24 -126
- data/lib/core/facets/string/indexable.rb +50 -133
- data/lib/core/facets/string/interpolate.rb +24 -59
- data/lib/core/facets/string/natcmp.rb +5 -39
- data/lib/core/facets/string/nchar.rb +37 -45
- data/lib/core/facets/string/op.rb +13 -0
- data/lib/core/facets/string/partitions.rb +38 -99
- data/lib/core/facets/string/range.rb +8 -49
- data/lib/core/facets/string/regesc.rb +9 -42
- data/lib/core/facets/string/scan.rb +6 -52
- data/lib/core/facets/string/splice.rb +8 -13
- data/lib/core/facets/string/tabs.rb +16 -153
- data/lib/core/facets/symbol.rb +3 -1
- data/lib/core/facets/symbol/chomp.rb +11 -29
- data/lib/core/facets/symbol/generate.rb +4 -43
- data/lib/core/facets/symbol/not.rb +13 -25
- data/lib/core/facets/symbol/shadow.rb +2 -40
- data/lib/core/facets/symbol/succ.rb +14 -13
- data/lib/core/facets/symbol/to_proc.rb +33 -28
- data/lib/core/facets/time.rb +1 -0
- data/lib/core/facets/time/change.rb +5 -38
- data/lib/core/facets/time/elapse.rb +2 -41
- data/lib/core/facets/time/stamp.rb +47 -0
- data/lib/core/facets/unboundmethod.rb +3 -0
- data/lib/core/facets/unboundmethod/arguments.rb +27 -4
- data/lib/core/facets/unboundmethod/name.rb +21 -7
- data/lib/methods/facets/enumerable/cluster_by.rb +1 -1
- data/lib/methods/facets/enumerable/collate.rb +1 -0
- data/lib/methods/facets/enumerable/divide.rb +1 -1
- data/lib/methods/facets/enumerable/each_by.rb +1 -1
- data/lib/methods/facets/enumerable/each_pair.rb +1 -1
- data/lib/methods/facets/enumerable/eachn.rb +1 -1
- data/lib/methods/facets/enumerable/graph.rb +1 -1
- data/lib/methods/facets/enumerable/group_by.rb +1 -1
- data/lib/methods/facets/enumerable/inject.rb +1 -0
- data/lib/methods/facets/enumerable/map_send.rb +1 -0
- data/lib/methods/facets/enumerable/modulate.rb +1 -0
- data/lib/methods/facets/enumerable/partition_by.rb +1 -1
- data/lib/methods/facets/hash/collate.rb +1 -1
- data/lib/methods/facets/hash/except.rb +1 -0
- data/lib/methods/facets/hash/graph.rb +1 -1
- data/lib/methods/facets/hash/mash.rb +1 -0
- data/lib/methods/facets/kernel/Bit.rb +1 -0
- data/lib/methods/facets/kernel/complete.rb +1 -1
- data/lib/methods/facets/kernel/here.rb +1 -1
- data/lib/methods/facets/kernel/non_nil.rb +1 -0
- data/lib/methods/facets/kernel/respond_with_value.rb +1 -0
- data/lib/methods/facets/kernel/with.rb +1 -1
- data/lib/methods/facets/module/class_def.rb +1 -0
- data/lib/methods/facets/string/camelcase.rb +1 -0
- data/lib/methods/facets/string/op_minus.rb +1 -0
- data/lib/methods/facets/string/snakecase.rb +1 -0
- data/lib/more/facets/attributes.rb +5 -5
- data/lib/more/facets/basicobject.rb +0 -62
- data/lib/{core/facets/continuation/create.rb → more/facets/continuation.rb} +4 -21
- data/lib/more/facets/duration.rb +534 -0
- data/lib/more/facets/lazy.rb +3 -3
- data/lib/more/facets/namespace.rb +23 -71
- data/lib/more/facets/rbsystem.rb +42 -8
- data/lib/more/facets/stylize.rb +6 -3
- data/lib/more/facets/thread.rb +55 -0
- data/meta/MANIFEST +49 -18
- data/meta/ROLLRC +1 -1
- data/meta/project.yaml +2 -2
- data/task/install +1 -1
- data/task/test/general +8 -2
- data/test/unit/array/test_pad.rb +30 -45
- data/test/unit/array/test_stackable.rb +14 -28
- data/test/unit/binding/test_cflow.rb +34 -49
- data/test/unit/binding/test_defined.rb +17 -0
- data/test/unit/binding/test_eval.rb +11 -39
- data/test/unit/binding/test_here.rb +17 -0
- data/test/unit/binding/test_self.rb +17 -0
- data/test/unit/binding/test_vars.rb +22 -37
- data/test/unit/class/test_descendents.rb +15 -30
- data/test/unit/class/test_initializer.rb +1 -8
- data/test/unit/comparable/test_bound.rb +38 -53
- data/test/unit/comparable/test_cmp.rb +45 -60
- data/test/unit/dir/test_traverse.rb +56 -0
- data/test/unit/enumerable/test_cartesian.rb +34 -47
- data/test/unit/enumerable/test_collect.rb +16 -125
- data/test/unit/enumerable/test_combination.rb +1 -8
- data/test/unit/enumerable/test_count.rb +51 -66
- data/test/unit/enumerable/test_each.rb +77 -0
- data/test/unit/enumerable/test_mash.rb +51 -0
- data/test/unit/enumerable/test_permutation.rb +1 -7
- data/test/unit/enumerable/test_probability.rb +42 -55
- data/test/unit/enumerable/test_split.rb +52 -0
- data/test/unit/exception/test_detail.rb +19 -0
- data/test/unit/file/test_read.rb +34 -0
- data/test/unit/file/test_topath.rb +17 -25
- data/test/unit/file/test_write.rb +6 -12
- data/test/unit/filetest/test_root.rb +14 -0
- data/test/unit/hash/test_at.rb +9 -21
- data/test/unit/hash/test_delete.rb +22 -37
- data/test/unit/hash/test_has_keys.rb +13 -28
- data/test/unit/hash/test_insert.rb +9 -23
- data/test/unit/hash/test_inverse.rb +11 -26
- data/test/unit/hash/test_iterate.rb +16 -31
- data/test/unit/hash/test_keyize.rb +40 -55
- data/test/unit/hash/test_merge.rb +10 -25
- data/test/unit/hash/test_new.rb +9 -24
- data/test/unit/hash/test_op.rb +1 -7
- data/test/unit/hash/test_rekey.rb +47 -62
- data/test/unit/hash/test_traverse.rb +17 -32
- data/test/unit/hash/test_update.rb +30 -45
- data/test/unit/hash/test_weave.rb +10 -23
- data/test/unit/integer/test_bitmask.rb +48 -44
- data/test/unit/integer/test_factorial.rb +11 -26
- data/test/unit/integer/test_multiples.rb +1 -5
- data/test/unit/integer/test_of.rb +14 -29
- data/test/unit/kernel/test_callstack.rb +13 -28
- data/test/unit/kernel/test_constant.rb +14 -28
- data/test/unit/kernel/test_deepcopy.rb +20 -35
- data/test/unit/kernel/test_instance.rb +46 -61
- data/test/unit/kernel/test_metaid.rb +42 -57
- data/test/unit/kernel/test_object.rb +14 -29
- data/test/unit/kernel/test_populate.rb +38 -53
- data/test/unit/kernel/test_report.rb +1 -7
- data/test/unit/kernel/test_returning.rb +16 -0
- data/test/unit/kernel/test_silence.rb +8 -23
- data/test/unit/kernel/test_super.rb +41 -56
- data/test/unit/kernel/test_tap.rb +15 -39
- data/test/unit/kernel/test_val.rb +7 -20
- data/test/unit/kernel/test_withattr.rb +29 -44
- data/test/unit/matchdata/test_matchset.rb +20 -35
- data/test/unit/module/test_abstract.rb +15 -30
- data/test/unit/module/test_alias.rb +32 -45
- data/test/unit/module/test_cattr.rb +39 -54
- data/test/unit/module/test_clone.rb +31 -43
- data/test/unit/module/test_include.rb +1 -35
- data/test/unit/module/test_modify.rb +102 -114
- data/test/unit/module/test_name.rb +1 -7
- data/test/unit/numeric/test_round.rb +46 -61
- data/test/unit/proc/test_bind.rb +29 -44
- data/test/unit/proc/test_compose.rb +20 -35
- data/test/unit/range/test_combine.rb +14 -29
- data/test/unit/range/test_overlap.rb +34 -51
- data/test/unit/regexp/test_arity.rb +14 -29
- data/test/unit/string/test_align.rb +13 -27
- data/test/unit/string/test_blank.rb +12 -27
- data/test/unit/string/test_bracket.rb +52 -67
- data/test/unit/string/test_case.rb +19 -38
- data/test/unit/string/test_crypt.rb +10 -25
- data/test/unit/string/test_filter.rb +1 -7
- data/test/unit/string/test_format.rb +2 -7
- data/test/unit/string/test_indexable.rb +54 -69
- data/test/unit/string/test_interpolate.rb +8 -21
- data/test/unit/string/test_natcmp.rb +11 -26
- data/test/unit/string/test_nchar.rb +1 -14
- data/test/unit/string/test_op.rb +14 -0
- data/test/unit/string/test_partitions.rb +39 -54
- data/test/unit/string/test_range.rb +17 -32
- data/test/unit/string/test_regesc.rb +1 -7
- data/test/unit/string/test_scan.rb +23 -38
- data/test/unit/string/test_stackable.rb +79 -98
- data/test/unit/string/test_tabs.rb +1 -7
- data/test/unit/symbol/test_chomp.rb +10 -21
- data/test/unit/symbol/test_generate.rb +7 -22
- data/test/unit/symbol/test_not.rb +10 -25
- data/test/unit/symbol/test_shadow.rb +8 -23
- data/test/unit/symbol/test_succ.rb +1 -7
- data/test/unit/symbol/test_to_proc.rb +1 -7
- data/test/unit/test_attributes.rb +1 -1
- data/test/unit/test_continuation.rb +13 -0
- data/test/unit/test_conversion.rb +1 -14
- data/test/unit/test_namespace.rb +30 -3
- data/test/unit/test_thread.rb +23 -0
- data/test/unit/time/test_change.rb +11 -26
- data/test/unit/time/test_elapse.rb +10 -25
- data/test/unit/time/test_stamp.rb +28 -0
- metadata +65 -25
- data/lib/core/facets/array/unzip.rb +0 -14
- data/lib/core/facets/continuation.rb +0 -1
- data/lib/core/facets/enumerable/collate.rb +0 -104
- data/lib/core/facets/kernel/require_esc.rb +0 -44
- data/lib/more/facets/mapsend.rb +0 -98
- data/lib/more/facets/pp_s.rb +0 -30
- data/log/history.rd +0 -38
- data/log/todo.rd +0 -4
- data/task/special/quickopts +0 -15
- data/test/unit/continuation/test_create.rb +0 -28
- data/test/unit/enumerable/test_collate.rb +0 -51
- data/test/unit/kernel/test_require_esc.rb +0 -29
- data/test/unit/test_mapsend.rb +0 -18
- data/test/unit/test_pp_s.rb +0 -17
@@ -1,12 +1,3 @@
|
|
1
|
-
# TITLE:
|
2
|
-
#
|
3
|
-
# Kernel Constant
|
4
|
-
#
|
5
|
-
# DESCRIPTION:
|
6
|
-
#
|
7
|
-
# Get a constant. But more convenient than const_get.
|
8
|
-
|
9
|
-
#
|
10
1
|
module Kernel
|
11
2
|
|
12
3
|
# This is similar to +Module#const_get+ but is accessible at all levels,
|
@@ -22,6 +13,8 @@ module Kernel
|
|
22
13
|
# Test.constant("Unit::Assertions") # -> Test::Unit::Assertions
|
23
14
|
# Test.constant("::Test::Unit") # -> Test::Unit
|
24
15
|
#
|
16
|
+
# CREDIT: Trans
|
17
|
+
|
25
18
|
def constant(const)
|
26
19
|
const = const.to_s.dup
|
27
20
|
base = const.sub!(/^::/, '') ? Object : ( self.kind_of?(Module) ? self : self.class )
|
@@ -29,32 +22,3 @@ module Kernel
|
|
29
22
|
end
|
30
23
|
|
31
24
|
end
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
# _____ _
|
36
|
-
# |_ _|__ ___| |_
|
37
|
-
# | |/ _ \/ __| __|
|
38
|
-
# | | __/\__ \ |_
|
39
|
-
# |_|\___||___/\__|
|
40
|
-
#
|
41
|
-
=begin test
|
42
|
-
|
43
|
-
require 'test/unit'
|
44
|
-
|
45
|
-
class TCKernel < Test::Unit::TestCase
|
46
|
-
|
47
|
-
def test_constant
|
48
|
-
c = ::Test::Unit::TestCase.name
|
49
|
-
assert_equal( ::Test::Unit::TestCase, constant(c) )
|
50
|
-
c = "Test::Unit::TestCase"
|
51
|
-
assert_equal( ::Test::Unit::TestCase, constant(c) )
|
52
|
-
c = "Unit::TestCase"
|
53
|
-
assert_equal( ::Test::Unit::TestCase, Test.constant(c) )
|
54
|
-
c = "TestCase"
|
55
|
-
assert_equal( ::Test::Unit::TestCase, Test::Unit.constant(c) )
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
59
|
-
|
60
|
-
=end
|
@@ -1,19 +1,3 @@
|
|
1
|
-
# TITLE:
|
2
|
-
#
|
3
|
-
# Copy Extensions
|
4
|
-
#
|
5
|
-
# FILE:
|
6
|
-
#
|
7
|
-
# kernel/copy.rb
|
8
|
-
#
|
9
|
-
# DESCRIPTION:
|
10
|
-
#
|
11
|
-
# Kernel extensions for copying objects.
|
12
|
-
#
|
13
|
-
# AUTHORS:
|
14
|
-
#
|
15
|
-
# CREDIT Jan Molic
|
16
|
-
|
17
1
|
module Kernel
|
18
2
|
|
19
3
|
# Anything that can be marshaled can be copied in totality.
|
@@ -25,7 +9,7 @@ module Kernel
|
|
25
9
|
Marshal::load(Marshal::dump(self))
|
26
10
|
end
|
27
11
|
|
28
|
-
|
12
|
+
alias_method :deep_copy, :copy
|
29
13
|
|
30
14
|
# Adds deep_clone method to an object which produces deep copy of it. It means
|
31
15
|
# if you clone a Hash, every nested items and their nested items will be cloned.
|
@@ -37,6 +21,8 @@ module Kernel
|
|
37
21
|
# obj.push( 'foo' )
|
38
22
|
# p obj # >> [ 'foo' ]
|
39
23
|
# p b[2] # >> []
|
24
|
+
#
|
25
|
+
# CREDIT: Jan Molic
|
40
26
|
|
41
27
|
def deep_clone( obj=self, cloned={} )
|
42
28
|
if cloned.has_key?( obj.object_id )
|
@@ -71,41 +57,3 @@ module Kernel
|
|
71
57
|
end
|
72
58
|
|
73
59
|
end
|
74
|
-
|
75
|
-
|
76
|
-
# _____ _
|
77
|
-
# |_ _|__ ___| |_
|
78
|
-
# | |/ _ \/ __| __|
|
79
|
-
# | | __/\__ \ |_
|
80
|
-
# |_|\___||___/\__|
|
81
|
-
#
|
82
|
-
=begin test
|
83
|
-
|
84
|
-
require 'test/unit'
|
85
|
-
|
86
|
-
class TestKernelCopy < Test::Unit::TestCase
|
87
|
-
|
88
|
-
# fixtures for copy / deep_copy
|
89
|
-
class A
|
90
|
-
attr_reader :a
|
91
|
-
def initialize
|
92
|
-
@a = 1
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
class B
|
97
|
-
attr_reader :b
|
98
|
-
def initialize
|
99
|
-
@b = A.new
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_deep_copy
|
104
|
-
o = B.new
|
105
|
-
oc = o.deep_copy
|
106
|
-
assert_equal( 1, o.b.a )
|
107
|
-
end
|
108
|
-
|
109
|
-
end
|
110
|
-
|
111
|
-
=end
|
@@ -12,7 +12,7 @@ module Kernel
|
|
12
12
|
#
|
13
13
|
# This is like #tap, but tap yields self -and- returns self.
|
14
14
|
#
|
15
|
-
#
|
15
|
+
# CREDIT: Daniel DeLorme
|
16
16
|
|
17
17
|
def ergo &b
|
18
18
|
if block_given?
|
@@ -31,7 +31,7 @@ class NilClass
|
|
31
31
|
# "a".ergo{ |o| o.upcase } #=> "A"
|
32
32
|
# nil.ergo{ |o| o.bar } #=> nil
|
33
33
|
#
|
34
|
-
#
|
34
|
+
# CREDIT: Daniel DeLorme
|
35
35
|
|
36
36
|
def ergo
|
37
37
|
@_ergo ||= Functor.new{ nil }
|
@@ -1,43 +1,31 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
# FILE:
|
6
|
-
#
|
7
|
-
# kernel/instance.rb
|
8
|
-
#
|
9
|
-
# DESCRIPTION:
|
10
|
-
#
|
11
|
-
# Kernel extension prefixed by instance_ which provide
|
12
|
-
# internal (eg private) access to the object.
|
13
|
-
#
|
14
|
-
# AUTHORS:
|
15
|
-
#
|
16
|
-
# CREDIT Thomas Sawyer
|
17
|
-
#
|
18
|
-
# NOTES:
|
19
|
-
#
|
20
|
-
# TODO Should instance_assign be named instance_variable_assign?
|
21
|
-
# Likewise for instance_assume. Is there a better term than 'assume'?
|
22
|
-
#
|
23
|
-
# TODO How to handle instance_exec? It's require proc/bind to work
|
24
|
-
# But that requires thread which we don't want to load automatically.
|
25
|
-
#
|
26
|
-
# TODO Should the __shadow__ methods be defined elsewhere?
|
27
|
-
|
28
|
-
require 'facets/proc/bind'
|
29
|
-
|
30
|
-
#
|
1
|
+
unless (RUBY_VERSION[0,3] == '1.9')
|
2
|
+
require 'facets/proc/bind'
|
3
|
+
end
|
4
|
+
|
31
5
|
module Kernel
|
6
|
+
#
|
7
|
+
# Kernel extension prefixed by instance_ which provide
|
8
|
+
# internal (eg private) access to the object.
|
9
|
+
#
|
10
|
+
|
11
|
+
unless (RUBY_VERSION[0,3] == '1.9')
|
12
|
+
# Like instance_eval but allows parameters to be passed.
|
13
|
+
#
|
14
|
+
# TODO: Remove for Ruby 1.9.
|
15
|
+
|
16
|
+
def instance_exec(*arguments, &block)
|
17
|
+
block.bind(self)[*arguments]
|
18
|
+
end
|
19
|
+
end
|
32
20
|
|
33
21
|
# Shadow method for instance_variable_get.
|
34
|
-
|
22
|
+
alias_method :__get__, :instance_variable_get
|
35
23
|
|
36
24
|
# Shadow method for instance_variable_set.
|
37
|
-
|
25
|
+
alias_method :__set__, :instance_variable_set
|
38
26
|
|
39
27
|
# Private send. Why isn't #send called instance_send in the first place?
|
40
|
-
|
28
|
+
alias_method :instance_send, :send
|
41
29
|
|
42
30
|
# Easy access to an object qua class, otherwise known
|
43
31
|
# as the object's metaclass or singleton class. This
|
@@ -46,16 +34,27 @@ module Kernel
|
|
46
34
|
#
|
47
35
|
# It is what it is.
|
48
36
|
#
|
49
|
-
#
|
50
|
-
|
37
|
+
# CREDIT: Trans
|
38
|
+
|
51
39
|
def instance_class( &block )
|
52
40
|
(class << self; self; end).module_eval(&block) if block
|
53
41
|
(class << self; self; end)
|
54
42
|
end
|
55
43
|
|
56
|
-
# CREDIT David Heinemeier Hansson
|
57
|
-
|
58
44
|
# Return instance variable values in an array.
|
45
|
+
#
|
46
|
+
# class X
|
47
|
+
# def initialize(a,b)
|
48
|
+
# @a, @b = a, b
|
49
|
+
# end
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
# x = X.new(1,2)
|
53
|
+
#
|
54
|
+
# x.instance_values #=> [1,2]
|
55
|
+
#
|
56
|
+
# CREDIT: David Heinemeier Hansson
|
57
|
+
|
59
58
|
def instance_values
|
60
59
|
instance_variables.inject({}) do |values, name|
|
61
60
|
values[name[1..-1]] = instance_variable_get(name)
|
@@ -63,12 +62,6 @@ module Kernel
|
|
63
62
|
end
|
64
63
|
end
|
65
64
|
|
66
|
-
# Like instace_eval but allows parameters to be passed.
|
67
|
-
#
|
68
|
-
def instance_exec(*arguments, &block)
|
69
|
-
block.bind(self)[*arguments]
|
70
|
-
end
|
71
|
-
|
72
65
|
# Assign instance vars using another object.
|
73
66
|
#
|
74
67
|
# class O
|
@@ -88,6 +81,8 @@ module Kernel
|
|
88
81
|
# o2.instance_eval{ @b } #=> 2
|
89
82
|
# o2.instance_eval{ @c } #=> 3
|
90
83
|
# o2.instance_eval{ @d } #=> 4
|
84
|
+
#
|
85
|
+
# CREDIT: Trans
|
91
86
|
|
92
87
|
def instance_assume(obj, *vars)
|
93
88
|
if vars.empty?
|
@@ -104,11 +99,17 @@ module Kernel
|
|
104
99
|
return self # ???
|
105
100
|
end
|
106
101
|
|
107
|
-
# For backward compatability (DEPRECATED).
|
108
|
-
|
102
|
+
# For backward compatability (TO BE DEPRECATED).
|
103
|
+
|
104
|
+
def assign_from(*args)
|
105
|
+
warn "use instance_assume for a future version"
|
106
|
+
instance_assume(*args)
|
107
|
+
end
|
109
108
|
|
110
109
|
# As with #instance_assume, but forces the setting of the object's
|
111
|
-
# instance varaibles even if the
|
110
|
+
# instance varaibles even if the reciever doesn't have them defined.
|
111
|
+
#
|
112
|
+
# See #instance_assume.
|
112
113
|
|
113
114
|
def instance_assume!(obj, *vars)
|
114
115
|
if vars.empty?
|
@@ -135,8 +136,10 @@ module Kernel
|
|
135
136
|
# @b #=> 2
|
136
137
|
#
|
137
138
|
#--
|
138
|
-
# TODO Make a little more flexiable to allow any hash-like object.
|
139
|
-
# TODO Should is also accept class variables? (eg. @@a)
|
139
|
+
# TODO: Make a little more flexiable to allow any hash-like object.
|
140
|
+
# TODO: Should is also accept class variables? (eg. @@a)
|
141
|
+
# TODO: Should instance_assign be named instance_variable_assign?
|
142
|
+
# Likewise for instance_assume. Is there a better term than 'assume'?
|
140
143
|
#++
|
141
144
|
|
142
145
|
def instance_assign(*args)
|
@@ -171,75 +174,3 @@ module Kernel
|
|
171
174
|
# For backward compatability (DEPRECATED).
|
172
175
|
alias :assign_with :instance_assign
|
173
176
|
end
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
# _____ _
|
178
|
-
# |_ _|__ ___| |_
|
179
|
-
# | |/ _ \/ __| __|
|
180
|
-
# | | __/\__ \ |_
|
181
|
-
# |_|\___||___/\__|
|
182
|
-
#
|
183
|
-
=begin test
|
184
|
-
|
185
|
-
require 'test/unit'
|
186
|
-
|
187
|
-
class TestKernelInstance < Test::Unit::TestCase
|
188
|
-
|
189
|
-
def test_instance_class
|
190
|
-
o = Object.new
|
191
|
-
assert_equal( (class << o; self; end), o.instance_class )
|
192
|
-
end
|
193
|
-
|
194
|
-
def test_instance_assign
|
195
|
-
ahash = { "z"=>0, "@a"=>1, "@b"=>2 } #, "@@a"=>3 }
|
196
|
-
instance_assign( ahash )
|
197
|
-
assert_equal( 0, @z )
|
198
|
-
assert_equal( 1, @a )
|
199
|
-
assert_equal( 2, @b )
|
200
|
-
#assert_equal( 3, @@a )
|
201
|
-
end
|
202
|
-
|
203
|
-
end
|
204
|
-
|
205
|
-
class TestInstanceEvalWithArgs < Test::Unit::TestCase
|
206
|
-
|
207
|
-
class Dummy
|
208
|
-
def f
|
209
|
-
:dummy_value
|
210
|
-
end
|
211
|
-
end
|
212
|
-
|
213
|
-
def test_instance_exec
|
214
|
-
# Create a block that returns the value of an argument
|
215
|
-
# and a value of a method call to +self+.
|
216
|
-
block = lambda { |a| [a, f] }
|
217
|
-
assert_equal [:arg_value, :dummy_value],
|
218
|
-
Dummy.new.instance_exec(:arg_value, &block)
|
219
|
-
end
|
220
|
-
|
221
|
-
def test_instance_exec_with_frozen_obj
|
222
|
-
block = lambda { |a| [a, f] }
|
223
|
-
obj = Dummy.new
|
224
|
-
obj.freeze
|
225
|
-
assert_equal [:arg_value, :dummy_value], obj.instance_exec(:arg_value, &block)
|
226
|
-
end
|
227
|
-
|
228
|
-
def test_instance_exec_nested
|
229
|
-
i = 0
|
230
|
-
obj = Dummy.new
|
231
|
-
block = lambda do |arg|
|
232
|
-
[arg] + instance_exec(1){|a| [f, a] }
|
233
|
-
end
|
234
|
-
assert_equal([:arg_value, :dummy_value, 1], obj.instance_exec(:arg_value, &block))
|
235
|
-
end
|
236
|
-
|
237
|
-
def test_instance_exec_with_immediate_value
|
238
|
-
obj = 1
|
239
|
-
block = lambda { |a| [a,self] }
|
240
|
-
assert_equal(["A", 1], obj.instance_exec("A", &block))
|
241
|
-
end
|
242
|
-
|
243
|
-
end
|
244
|
-
|
245
|
-
=end
|
@@ -1,16 +1,3 @@
|
|
1
|
-
# TITLE:
|
2
|
-
#
|
3
|
-
# Metaid
|
4
|
-
#
|
5
|
-
# SUMMARY:
|
6
|
-
#
|
7
|
-
# Meta programming methods, for accessing
|
8
|
-
# object singleton class.
|
9
|
-
#
|
10
|
-
# AUTHORS:
|
11
|
-
#
|
12
|
-
# CREDIT WhyTheLuckyStiff
|
13
|
-
|
14
1
|
require 'facets/functor'
|
15
2
|
|
16
3
|
module Kernel
|
@@ -24,6 +11,8 @@ module Kernel
|
|
24
11
|
#
|
25
12
|
# X.a = 1
|
26
13
|
# X.a #=> 1
|
14
|
+
#
|
15
|
+
# CREDIT: Trans
|
27
16
|
|
28
17
|
def meta
|
29
18
|
@_meta_functor ||= Functor.new do |op,*args|
|
@@ -32,6 +21,12 @@ module Kernel
|
|
32
21
|
end
|
33
22
|
|
34
23
|
# Alias a method defined in the metaclass (ie. singleton class).
|
24
|
+
#
|
25
|
+
# def X.y?; "y?" ; end
|
26
|
+
# X.meta_alias "ynot?", "y?"
|
27
|
+
# X.ynot? #=> y?
|
28
|
+
#
|
29
|
+
# CREDIT: Trans
|
35
30
|
|
36
31
|
def meta_alias(*args)
|
37
32
|
meta_class do
|
@@ -39,17 +34,26 @@ module Kernel
|
|
39
34
|
end
|
40
35
|
end
|
41
36
|
|
42
|
-
# Evaluate code in a metaclass.
|
37
|
+
# Evaluate code in a metaclass. This is equivalent to
|
38
|
+
# 'meta_class.instance_eval'.
|
39
|
+
#
|
40
|
+
# CREDIT: WhyTheLuckyStiff
|
43
41
|
|
44
|
-
def meta_eval(
|
42
|
+
def meta_eval(str=nil, &blk)
|
45
43
|
if str
|
46
|
-
meta_class.instance_eval(
|
44
|
+
meta_class.instance_eval(str)
|
47
45
|
else
|
48
|
-
meta_class.instance_eval(
|
46
|
+
meta_class.instance_eval(&blk)
|
49
47
|
end
|
50
48
|
end
|
51
49
|
|
52
|
-
# Add method to a meta-class.
|
50
|
+
# Add method to a meta-class --i.e. a singleton method.
|
51
|
+
#
|
52
|
+
# class X; end
|
53
|
+
# X.meta_def(:x){"x"}
|
54
|
+
# X.x #=> "x"
|
55
|
+
#
|
56
|
+
# CREDIT: WhyTheLuckyStiff
|
53
57
|
|
54
58
|
def meta_def( name, &block )
|
55
59
|
meta_class do
|
@@ -57,12 +61,6 @@ module Kernel
|
|
57
61
|
end
|
58
62
|
end
|
59
63
|
|
60
|
-
# Defines an instance method within a class.
|
61
|
-
|
62
|
-
def class_def name, &blk
|
63
|
-
class_eval { define_method name, &blk }
|
64
|
-
end
|
65
|
-
|
66
64
|
# Easy access to an object's "special" class,
|
67
65
|
# otherwise known as it's metaclass or singleton class.
|
68
66
|
|
@@ -85,6 +83,8 @@ module Kernel
|
|
85
83
|
#
|
86
84
|
# -- why the lucky stiff
|
87
85
|
#
|
86
|
+
# CREDIT: WhyTheLuckyStiff
|
87
|
+
|
88
88
|
def eigenclass
|
89
89
|
(class << self; self; end)
|
90
90
|
end
|
@@ -112,6 +112,8 @@ module Kernel
|
|
112
112
|
# known as the object's metaclass or singleton class.
|
113
113
|
#
|
114
114
|
# Yes, another one.
|
115
|
+
#
|
116
|
+
# CREDIT: Trans
|
115
117
|
|
116
118
|
def qua_class(&block)
|
117
119
|
if block_given?
|
@@ -124,6 +126,8 @@ module Kernel
|
|
124
126
|
# Universal assignment. This is a meta-programming
|
125
127
|
# method, which allows you to assign any type of variable.
|
126
128
|
#
|
129
|
+
# CREDIT: Trans
|
130
|
+
|
127
131
|
def __assign__(name, value)
|
128
132
|
k = name.to_s
|
129
133
|
v = value
|
@@ -143,56 +147,14 @@ module Kernel
|
|
143
147
|
end
|
144
148
|
|
145
149
|
|
146
|
-
|
147
|
-
# |_ _|__ ___| |_
|
148
|
-
# | |/ _ \/ __| __|
|
149
|
-
# | | __/\__ \ |_
|
150
|
-
# |_|\___||___/\__|
|
151
|
-
#
|
152
|
-
=begin test
|
150
|
+
class Module
|
153
151
|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
def test_metaclass
|
159
|
-
o = Object.new
|
160
|
-
assert_equal( (class << o; self; end), o.metaclass )
|
161
|
-
end
|
162
|
-
|
163
|
-
def test_meta_class
|
164
|
-
o = Object.new
|
165
|
-
assert_equal( (class << o; self; end), o.meta_class )
|
166
|
-
end
|
167
|
-
|
168
|
-
def test_singleton
|
169
|
-
o = Object.new
|
170
|
-
assert_equal( (class << o; self; end), o.singleton )
|
171
|
-
end
|
172
|
-
|
173
|
-
#def test_singleton_eval
|
174
|
-
# assert_nothing_raised do
|
175
|
-
# o.singleton_class_eval{ @@a = "test" }
|
176
|
-
# end
|
177
|
-
#end
|
178
|
-
|
179
|
-
#def test_singleton_method
|
180
|
-
# assert_nothing_raised do
|
181
|
-
# o.define_singleton_method(:testing){ |x| x + 1 }
|
182
|
-
# end
|
183
|
-
# assert_equal(2, o.testing(1) )
|
184
|
-
#end
|
185
|
-
|
186
|
-
def test_singleton_Class
|
187
|
-
o = Object.new
|
188
|
-
assert_equal( (class << o; self; end), o.singleton_class )
|
189
|
-
end
|
190
|
-
|
191
|
-
def test_qua_class
|
192
|
-
o = Object.new
|
193
|
-
assert_equal( (class << o; self; end), o.qua_class )
|
194
|
-
end
|
152
|
+
# Defines an instance method within a class.
|
153
|
+
#
|
154
|
+
# CREDIT: WhyTheLuckyStiff
|
195
155
|
|
156
|
+
def class_def name, &blk
|
157
|
+
class_eval { define_method name, &blk }
|
196
158
|
end
|
197
159
|
|
198
|
-
|
160
|
+
end
|